Why does JavaScript Promise then handler run after other code?

The relevant specs are here: Promises/A+ point 2.2.4: onFulfilled or onRejected must not be called until the execution context stack contains only platform code. [3.1]. And note 3.1 (emphasis mine): Here “platform code” means engine, environment, and promise implementation code. In practice, this requirement ensures that onFulfilled and onRejected execute asynchronously, after the event loop … Read more

ES6 Promise replacement of async.eachLimit / async.mapLimit

If you don’t care about the results, then it’s quick to whip one up: Promise.eachLimit = async (funcs, limit) => { let rest = funcs.slice(limit); await Promise.all(funcs.slice(0, limit).map(async func => { await func(); while (rest.length) { await rest.shift()(); } })); }; // Demo: var wait = ms => new Promise(resolve => setTimeout(resolve, ms)); async function … Read more

Expect item in array

Looks like you need a custom matcher. Depending on the version of Jasmine you are using: With Jasmine 1: this.addMatchers({ toBeIn: function(expected) { var possibilities = Array.isArray(expected) ? expected : [expected]; return possibilities.indexOf(this.actual) > -1; } }); With Jasmine 2: this.addMatchers({ toBeIn: function(util, customEqualityTesters) { return { compare: function(actual, expected) { var possibilities = Array.isArray(expected) … Read more

Rendering resolved promise value in Ember handlebars template

You could have the property lazily set itself, something like: App.TopicItem = DS.Model.extend({ topic: DS.belongsTo(‘topic’), paddedPosition: function(key, value) { if (arguments.length > 1) { // > 1 args = this is a `set` return value; } else { // otherwise this is a `get` var _this = this; value = null; this.get(‘topic.course.lessons’). then(function(lessons) { // … Read more

How to return a promise when writestream finishes?

You’ll want to use the Promise constructor: function writeToFile(filePath: string, arr: string[]): Promise<boolean> { return new Promise((resolve, reject) => { const file = fs.createWriteStream(filePath); for (const row of arr) { file.write(row + “\n”); } file.end(); file.on(“finish”, () => { resolve(true); }); // not sure why you want to pass a boolean file.on(“error”, reject); // don’t … Read more