2013-08-06 09:51:22 +00:00
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
2015-01-03 01:08:02 +00:00
|
|
|
var PromiseA = require('bluebird')
|
|
|
|
, forEachAsync = require('./forEachAsync').forEachAsync
|
|
|
|
, context = {}
|
2013-08-06 09:51:22 +00:00
|
|
|
;
|
|
|
|
|
2015-01-03 01:08:02 +00:00
|
|
|
forEachAsync([0, 500, 70, 200, 400, 100], function (element, i, arr) {
|
2015-01-03 01:39:27 +00:00
|
|
|
console.log(i, '/', arr.length, 'began');
|
|
|
|
|
|
|
|
var result
|
|
|
|
;
|
2013-08-06 10:00:37 +00:00
|
|
|
|
|
|
|
// test that thisness is applied
|
2013-08-06 09:51:22 +00:00
|
|
|
this[element] = i;
|
2013-08-06 10:00:37 +00:00
|
|
|
|
2015-01-03 01:39:27 +00:00
|
|
|
if (i % 2) {
|
2013-08-06 10:00:37 +00:00
|
|
|
// test that synchronous callbacks don't mess things up
|
2015-01-03 01:39:27 +00:00
|
|
|
result = PromiseA.resolve();
|
2013-08-06 10:00:37 +00:00
|
|
|
} else {
|
|
|
|
// test asynchronous callbacks
|
2015-01-03 01:39:27 +00:00
|
|
|
result = new Promise(function (resolve/*, reject*/) {
|
2015-01-03 01:08:02 +00:00
|
|
|
setTimeout(resolve, element);
|
|
|
|
});
|
2013-08-06 10:00:37 +00:00
|
|
|
}
|
2015-01-03 01:39:27 +00:00
|
|
|
|
|
|
|
return result.then(function () {
|
|
|
|
// test that array order is as expected
|
|
|
|
console.log(i, '/', arr.length, 'complete');
|
|
|
|
});
|
2015-01-03 01:08:02 +00:00
|
|
|
}, context).then(function () {
|
|
|
|
// test that thisness carried
|
|
|
|
console.log('context', context);
|
2013-08-06 10:00:37 +00:00
|
|
|
}).then(function () {
|
|
|
|
// test then chaining
|
|
|
|
console.log("now wasn't that nice?");
|
2013-08-06 09:51:22 +00:00
|
|
|
});
|
|
|
|
}());
|