Last active
September 9, 2016 06:12
-
-
Save 1yx/5788780a52fca97758b9627fdc664b34 to your computer and use it in GitHub Desktop.
This file contains 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'; | |
const Promise = require('bluebird'); | |
const _ = require('lodash'); | |
const t = Date.now(); | |
function delay(second, idx, lng) { | |
var deferred = Promise.pending(); | |
setTimeout(function() { | |
console.log('execute:', _.floor((Date.now() - t) / 1000) + 's'); | |
console.log('index is:', idx); | |
deferred.resolve('rsv: ' + second); | |
}, second * 1000); | |
return deferred.promise; | |
} | |
Promise.all(_.map([ | |
5, 4, 3, 2, 1 | |
], (itm, idx) => delay(itm, idx))).then((rst) => { | |
console.log('all returns:', rst); | |
console.log('all execute:', _.floor((Date.now() - t) / 1000) + 's'); | |
return; | |
}).catch((err) => { | |
console.log(err); | |
}); | |
/* | |
execute: 1s | |
index is: 4 | |
execute: 2s | |
index is: 3 | |
execute: 3s | |
index is: 2 | |
execute: 4s | |
index is: 1 | |
execute: 5s | |
index is: 0 | |
all returns: [ 'rsv: 5', 'rsv: 4', 'rsv: 3', 'rsv: 2', 'rsv: 1' ] | |
all execute: 5s | |
*/ |
This file contains 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
/* | |
Promise.all | |
Promise.props | |
Promise.any | |
Promise.some | |
Promise.map | |
Promise.reduce | |
Promise.filter | |
Promise.each | |
Promise.mapSeries | |
Promise.race | |
*/ |
This file contains 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'; | |
const Promise = require('bluebird'); | |
const _ = require('lodash'); | |
const t = Date.now(); | |
Promise.each([ | |
5, 4, 3, 2, 1 | |
], (second, idx, lng) => { | |
var deferred = Promise.pending(); | |
setTimeout(function() { | |
console.log('execute:', _.floor((Date.now() - t) / 1000) + 's'); | |
console.log('index is:', idx); | |
deferred.resolve('rsv: ' + second); | |
}, second * 1000); | |
return deferred.promise; | |
}).then((rst) => { | |
console.log('all returns:', rst); | |
console.log('all execute:', _.floor((Date.now() - t) / 1000) + 's'); | |
return; | |
}).catch((err) => { | |
console.log(err); | |
}); | |
/* | |
execute: 5s | |
index is: 0 | |
execute: 9s | |
index is: 1 | |
execute: 12s | |
index is: 2 | |
execute: 14s | |
index is: 3 | |
execute: 15s | |
index is: 4 | |
all returns: [ 5, 4, 3, 2, 1 ] // IMPORTANT | |
all execute: 15s | |
*/ |
This file contains 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'; | |
const Promise = require('bluebird'); | |
const _ = require('lodash'); | |
const t = Date.now(); | |
Promise.map([ | |
5, 4, 3, 2, 1 | |
], (second, idx, lng) => { | |
var deferred = Promise.pending(); | |
setTimeout(function() { | |
console.log('execute:', _.floor((Date.now() - t) / 1000) + 's'); | |
console.log('index is:', idx); | |
deferred.resolve('rsv: ' + second); | |
}, second * 1000); | |
return deferred.promise; | |
}).then((rst) => { | |
console.log('all returns:', rst); | |
console.log('all execute:', _.floor((Date.now() - t) / 1000) + 's'); | |
return; | |
}).catch((err) => { | |
console.log(err); | |
}); | |
/* | |
execute: 1s | |
index is: 4 | |
execute: 2s | |
index is: 3 | |
execute: 3s | |
index is: 2 | |
execute: 4s | |
index is: 1 | |
execute: 5s | |
index is: 0 | |
all returns: [ 'rsv: 5', 'rsv: 4', 'rsv: 3', 'rsv: 2', 'rsv: 1' ] | |
all execute: 5s | |
*/ |
This file contains 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'; | |
const Promise = require('bluebird'); | |
const _ = require('lodash'); | |
const t = Date.now(); | |
Promise.mapSeries([ | |
5, 4, 3, 2, 1 | |
], (second, idx, lng) => { | |
var deferred = Promise.pending(); | |
setTimeout(function() { | |
console.log('execute:', _.floor((Date.now() - t) / 1000) + 's'); | |
console.log('index is:', idx); | |
deferred.resolve('rsv: ' + second); | |
}, second * 1000); | |
return deferred.promise; | |
}).then((rst) => { | |
console.log('all returns:', rst); | |
console.log('all execute:', _.floor((Date.now() - t) / 1000) + 's'); | |
return; | |
}).catch((err) => { | |
console.log(err); | |
}); | |
/* | |
execute: 5s | |
index is: 0 | |
execute: 9s | |
index is: 1 | |
execute: 12s | |
index is: 2 | |
execute: 14s | |
index is: 3 | |
execute: 15s | |
index is: 4 | |
all returns: [ 'rsv: 5', 'rsv: 4', 'rsv: 3', 'rsv: 2', 'rsv: 1' ] | |
all execute: 15s | |
*/ |
This file contains 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'; | |
const Promise = require('bluebird'); | |
const _ = require('lodash'); | |
const t = Date.now(); | |
Promise.reduce([ | |
5, 4, 3, 2, 1 | |
], (accumulator, second, idx, lng) => { | |
var deferred = Promise.pending(); | |
setTimeout(function() { | |
console.log('last return:', accumulator); | |
console.log('execute:', _.floor((Date.now() - t) / 1000) + 's'); | |
console.log('index is:', idx); | |
deferred.resolve('rsv: ' + second); | |
}, second * 1000); | |
return deferred.promise; | |
}, 'init data').then((rst) => { | |
console.log('all returns:', rst); | |
console.log('all execute:', _.floor((Date.now() - t) / 1000) + 's'); | |
return; | |
}).catch((err) => { | |
console.log(err); | |
}); | |
/* | |
last return: init data | |
execute: 5s | |
index is: 0 | |
last return: rsv: 5 | |
execute: 9s | |
index is: 1 | |
last return: rsv: 4 | |
execute: 12s | |
index is: 2 | |
last return: rsv: 3 | |
execute: 14s | |
index is: 3 | |
last return: rsv: 2 | |
execute: 15s | |
index is: 4 | |
all returns: rsv: 1 // IMPORTANT | |
all execute: 15s | |
*/ |
This file contains 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'; | |
const Promise = require('bluebird'); | |
const _ = require('lodash'); | |
const t = Date.now(); | |
Promise.reduce([ | |
5, 4, 3, 2, 1 | |
], (accumulator, second, idx, lng) => { | |
var deferred = Promise.pending(); | |
setTimeout(function() { | |
console.log('last return:', accumulator); | |
console.log('execute:', _.floor((Date.now() - t) / 1000) + 's'); | |
console.log('index is:', idx); | |
deferred.resolve(5); | |
}, second * 1000); | |
return deferred.promise; | |
}).then((rst) => { | |
console.log('all returns:', rst); | |
console.log('all execute:', _.floor((Date.now() - t) / 1000) + 's'); | |
return; | |
}).catch((err) => { | |
console.log(err); | |
}); | |
/* | |
last return: 5 //IMPORTANT | |
execute: 4s | |
index is: 1 | |
last return: rsv: 4 | |
execute: 7s | |
index is: 2 | |
last return: rsv: 3 | |
execute: 9s | |
index is: 3 | |
last return: rsv: 2 | |
execute: 10s | |
index is: 4 | |
all returns: rsv: 1 // IMPORTANT | |
all execute: 10s // IMPORTANT | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment