2017-01-17 2 views
0

Ich habe einen Code, der im Grunde so etwas wie diesesJavascript - rufen zwei asynchrone Funktionen sequentiell

class foo { 
    constructor() { 

     // calling a, b, c synchronously 
     a().then(
      function(response) { 
       b().then(
        function(response) { 
         c(); 
        }, function(error) { 
         // pass 
        } 
       ); 
      }, function(error) { 
       // pass 
      } 
     ); 
    } 

    a() { 
     return new Promise(function(fullfill, reject) { 

      // x is any function that returns a promise, making the 
      // implementation of this function "a" asynchronous 
      x().then(
       function(response) { 
        fullfill(response); 
       }, function(error) { 
        reject(error); 
       } 
      ); 

     }); 
    } 

    b() { 
     return new Promise(function(fullfill, reject) { 

      // x is any function that returns a promise, making the 
      // implementation of this function "b" asynchronous 
      x().then(
       function(response) { 
        fullfill(response); 
       }, function(error) { 
        reject(error); 
       } 
      ); 

     }); 
    } 

    c() { 
     // do something 
    } 

} 

sieht, habe ich zwei Funktionen a und b die sowohl asynchron sind. Diese Funktionen sind asynchron, weil sie beide eine Funktion x aufrufen, die ein Versprechen zurückgibt (in meinem Fall ist es eine Abfrage an eine Datenbank).

Ich muss a anrufen, gefolgt von b gefolgt von c aber nacheinander. Ein Weg, dies zu tun, ist das, was ich im obigen Code implementiert habe, aber es führt zu verschachtelten Versprechensantworten.

Gibt es eine andere Möglichkeit, dass ich das gleiche Ergebnis erreichen kann, ohne die folgende Syntax zu verwenden (denn wenn das die einzige mögliche Lösung ist, dann könnte ich sie auch gar nicht verwenden).

+1

Verwenden Sie Versprechen Verkettung http://StackOverflow.com/Questions/34696696/chain-Promises-in-Javascript – M14

+0

@ATMD Ja. 'a',' b' und 'c' sind Funktionen, die ich geschrieben habe. Aber sie alle stellen Abfragen an eine Datenbank und diese Abfragen sind asynchron. Und ich habe keine Kontrolle über diese Fragen. – ironstein

+0

Was @m14 gesagt hat, mach einfach 'a(). Dann (b) .then (c);'. –

Antwort

1

Verwenden Versprechen Verkettungs wie unten dargestellt:

a().then(resA => { 
    // resA is response of function a() 
    // Do whatever you want with resA here 
    // and then return promise of function b() 

    return b(); 
}) 
.then(resB => { 
    // resB is response of function b() 
    // Do whatever you want with resA here 
    // and then return promise of function c() 

    return c(); 
}) 
.then(resC => { 
    // resC is response of function c() 
    // Do whatever you want with resC here 
}) 
.catch(err => { 
    // Handle any reject/error of functions a, b or c 
}); 

Wenn Sie die Antwort der Funktion a und b bis zum Ende führen wollen, dann können Sie die Verkettungs wie folgt vorgehen:

a().then(resA => { 
    // resA is response of function a() 
    // Do whatever you want with resA here 
    // and then return promise of function b() 

    return b().then(resB => { resA: resA, resB: resB }); 
}) 
.then(res => { 
    // res is a object with rsponse of function a() & b() 
    // res.resA is response of function a() 
    // res.resB is response of function b() 
    // Do whatever you want to with the responses here 
    // then return promise of function c() 

    return c().then(resC, { resA: res.resA, resB: res.resB, resC: resC }); 
}) 
.then(resC => { 
    // res is a object with rsponse of function a(), b() & c() 
    // res.resA is response of function a() 
    // res.resB is response of function b() 
    // res.resC is response of function c() 
    // Do whatever you want to with the responses 
}) 
.catch(err => { 
    // Handle any reject/error of functions a, b or c 
}); 

Die Der obige Code ist ausführlich für Demo/Erklärung. Es kann mit ES6 wie unten gezeigt

a().then(resA => { 
    // resA is response of function a() 
    // Do whatever you want with resA here 
    // and then return promise of function b() 

    return b().then(resB => { resA, resB }); 
}) 
.then(res => { 
    // res is a object with rsponse of function a() & b() 
    // res.resA is response of function a() 
    // res.resB is response of function b() 
    // Do whatever you want to with the responses here 
    // then return promise of function c() 

    return c().then(resC, Object,assign(res, { resC })); 
}) 
.then(resC => { 
    // res is a object with rsponse of function a(), b() & c() 
    // res.resA is response of function a() 
    // res.resB is response of function b() 
    // res.resC is response of function c() 
    // Do whatever you want to with the responses 
}) 
.catch(err => { 
    // Handle any reject/error of functions a, b or c 
}); 

Hope dies hilft.

0

Sei seq = [f1, f2, ..., fn] sei eine Folge von null-Argumente Versprechen zurückgebenden Funktionen. Erstellen Sie ein Versprechen p, das sie der Reihe nach mit dem Aufruf f nach f {i-1} ausführt.

var p = seq.reduce ( (p_acc, f) => p_acc.then (() => f()) )

Dann möchten Sie wahrscheinlich

p.catch tun (...)

Verwandte Themen