How do I wait for a promise to finish before returning the variable of a function?

I’m still struggling with promises, but making some progress thanks to the community here. I have a simple JS function which queries a Parse database. It’s supposed to return the array of results, but obviously due to the asynchronous nature of the query (hence the promises), the function returns before the results, leaving me with … Read more

Why does .json() return a promise?

I’ve been messing around with the fetch() api recently, and noticed something which was a bit quirky. let url = “http://jsonplaceholder.typicode.com/posts/6”; let iterator = fetch(url); iterator .then(response => { return { data: response.json(), status: response.status } }) .then(post => document.write(post.data)); ; post.data returns a Promise object. http://jsbin.com/wofulo/2/edit?js,output However if it is written as: let url … Read more

How can I synchronously determine a JavaScript Promise’s state?

I have a pure JavaScript Promise (built-in implementation or poly-fill): var promise = new Promise(function (resolve, reject) { /* … */ }); From the specification, a Promise can be one of: ‘settled’ and ‘resolved’ ‘settled’ and ‘rejected’ ‘pending’ I have a use case where I wish to interrogate the Promise synchronously and determine: is the … Read more

How to find which promises are unhandled in Node.js UnhandledPromiseRejectionWarning?

Node.js from version 7 has async/await syntactic sugar for handling promises and now in my code the following warning comes up quite often: (node:11057) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: Error: Can’t set headers after they are sent. (node:11057) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not … Read more

Why is my asynchronous function returning Promise { } instead of a value?

My code: let AuthUser = data => { return google.login(data.username, data.password).then(token => { return token } ) } And when i try to run something like this: let userToken = AuthUser(data) console.log(userToken) I’m getting: Promise { <pending> } But why? My main goal is to get token from google.login(data.username, data.password) which returns a promise, into … Read more

When is .then(success, fail) considered an antipattern for promises?

I had a look at the bluebird promise FAQ, in which it mentions that .then(success, fail) is an antipattern. I don’t quite understand its explanation as for the try and catch. What’s wrong with the following? some_promise_call() .then(function(res) { logger.log(res) }, function(err) { logger.log(err) }) It seems that the example is suggesting the following to … Read more

Difference between microtask and macrotask within an event loop context

I’ve just finished reading the Promises/A+ specification and stumbled upon the terms microtask and macrotask: see http://promisesaplus.com/#notes I’ve never heard of these terms before, and now I’m curious what the difference could be? I’ve already tried to find some information on the web, but all I’ve found is this post from the w3.org Archives (which … Read more