foreachasync.js/test-native.js

32 lines
855 B
JavaScript
Raw Normal View History

2013-08-06 09:51:22 +00:00
(function () {
"use strict";
2015-01-03 01:08:02 +00:00
var forEachAsync = require('./forEachAsync').forEachAsync.create(Promise)
, 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) {
// test that array order is as expected
2013-08-06 09:51:22 +00:00
console.log(element, 'is element', i, 'of', arr.length);
// test that thisness is applied
2013-08-06 09:51:22 +00:00
this[element] = i;
if (i > 2) {
// test that synchronous callbacks don't mess things up
2015-01-03 01:08:02 +00:00
return Promise.resolve();
} else {
// test asynchronous callbacks
2015-01-03 01:08:02 +00:00
return new Promise(function (resolve/*, reject*/) {
setTimeout(resolve, element);
});
}
2015-01-03 01:08:02 +00:00
}, context).then(function () {
// test that thisness carried
console.log('context', context);
}).then(function () {
// test then chaining
console.log("now wasn't that nice?");
2013-08-06 09:51:22 +00:00
});
}());