Created
March 3, 2017 03:11
-
-
Save kurunve/685e5229836948418644642922f4fe83 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var Promise = require('bluebird'); | |
class ChainThing { | |
constructor(param1, param2) { | |
this.time = param1; | |
this.message = param2; | |
} | |
step1() { | |
console.log('['+this.message+'] STEP 1 '); | |
return new Promise((resolve, reject) => { | |
setInterval(function() { | |
var err = ''; | |
if (err) { | |
return reject(err) | |
} else { | |
return resolve(); | |
} | |
}, this.time); | |
}) | |
} | |
step2() { | |
console.log('['+this.message+'] STEP 2 '); | |
return new Promise((resolve, reject) => { | |
setInterval(function() { | |
var err = ''; | |
if (err) { | |
return reject(err) | |
} else { | |
return resolve('Async data result'); | |
} | |
}, this.time); | |
}) | |
} | |
step3(param) { | |
console.log('['+this.message+'] STEP 3 '); | |
console.log('['+this.message+'] ' + param); | |
return new Promise((resolve, reject) => { | |
setInterval(function() { | |
var err = ''; | |
if (err) { | |
return reject(err) | |
} else { | |
return resolve(); | |
} | |
}, this.time); | |
}) | |
} | |
logAsCompleted() { | |
console.log('['+this.message+'] THE END '); | |
return new Promise((resolve, reject) => { | |
return resolve(); | |
}); | |
} | |
} | |
var array = | |
[ | |
new ChainThing(1290, 'v1'), | |
new ChainThing(920, 'v2'), | |
new ChainThing(1010, 'v3'), | |
new ChainThing(1500, 'v4') | |
]; | |
Promise.each(array, (p1) => { | |
return Promise.resolve(p1) | |
.then(p1.step1.bind(p1)) | |
.then(p1.step2.bind(p1)) | |
.then(p1.step3.bind(p1)) | |
.then(p1.logAsCompleted.bind(p1)); | |
} | |
) | |
.then( | |
function() { | |
return new Promise((resolve, reject) => { | |
console.log('FINALLY'); | |
resolve(); | |
}); | |
} | |
) | |
.catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment