Created
March 3, 2017 02:26
-
-
Save kurunve/ba2f018aac39f0347e4afd3698bb6e80 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 p1 = new ChainThing(1000, 'v1'); | |
p1.step1().then(p1.step1.bind(p1)) | |
.then(p1.step2.bind(p1)) | |
.then(p1.step3.bind(p1)) | |
.then(p1.logAsCompleted.bind(p1)); | |
var p2 = new ChainThing(1200, 'v3'); | |
p2.step1().then(p2.step1.bind(p2)) | |
.then(p2.step2.bind(p2)) | |
.then(p2.step3.bind(p2)) | |
.then(p2.logAsCompleted.bind(p2)); | |
var p3 = new ChainThing(2000, 'v3'); | |
p3.step1().then(p3.step1.bind(p3)) | |
.then(p3.step2.bind(p3)) | |
.then(p3.step3.bind(p3)) | |
.then(p3.logAsCompleted.bind(p3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment