Demo entry 6716202

promise js

   

Submitted by tiffon on Feb 16, 2018 at 16:49
Language: JavaScript. Code size: 699 Bytes.

// not good 
var p1 = api1();                // returning a promise 
var p3 = p1.then(api1Result => {
    var p2 = api2();            // returning a promise 
    return p2;                  // The result of p2 
});                             // becomes the result of p3 
 
// also not good 
api1().then(function(api1Result) {
    return api2().then(console.log)
})
 
// better 
api1().then(function(api1Result) {
    return api2();
}).then(console.log)
 
// best 
api1().then(api2).then(console.log);
 
// concurrently, logs both results 
Bluebird.join(api1(), api2(), (resut1, result2) => console.log(result1, result2));
// AKA 

Bluebird.join(api1(), api2(), console.log);

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).