23

I have a spec from a client for an implementation of a method in a module:

 // getGenres():
 //  Returns a promise. When it resolves, it returns an array.

If given an array of genres,

['comedy', 'drama', 'action']

Here is a skeleton method with a promise:

MovieLibrary.getGenres = function() {
  var promise = new Promise(function(resolve, reject) {
    /* missing implementation */
  });

  return promise;
};

Can the promise be made to return the data found in the genres? Is there a better way to achieve the spec description?

  • 7
    Promises don't "return" values, they pass them to a callback (which you supply with .then()). The spec simply sounds confused to me. It's probably trying to say that you're supposed to do resolve([genre1, genre2, ...]); inside the promise implementation. – Ixrec Apr 22 '15 at 23:07
23

It sounds like you aren't understanding how promises are used. You return a promise. Then, later when your code resolves the promise, it resolves it with a result and that result is passed to the .then() handler attached to the promise:

MovieLibrary.getGenres = function() {
  var promise = new Promise(function(resolve, reject) {
    /* missing implementation */
    resolve(result);
  });

  return promise;
};

MovieLibrary.getGenres().then(function(result) {
    // you can access the result from the promise here
});
  • 4
    This confirms that promises work the way that I expected. – sealocal Apr 22 '15 at 23:31
  • Probably, you don't understand why this might be needed. In environment, like React Native with Redux with Realm, I have to put initial state to Redux createStore. It should be fetched from Realm, but it returns promise. I just want to block until it returns data as this should be very fast and simple. Instead I get problems how to combine Realm promises and usual JSON object for createStore() – likern Dec 26 '18 at 19:19
17

Updated version using await rather than .then().

await stops executing until the Promise has resolved. Unlike using .then() you can just keep awaiting values as you do various things that return promises, and execution continues onto the next line (this is called 'direct style). It's also much nicer to look at , since it's consistent with the rest of JavaScript, than .then() everywhere.

// Example function that returns a Promise that will resolve after 2 seconds
var getGenres = function() {
  return new Promise(function(resolve) {
    setTimeout(function(){
      resolve(['comedy', 'drama', 'action'])
    }, 2000);
  });
}

// We start an 'async' function to use the 'await' keyword
(async function(){
  var result = await getGenres()
  console.log('Woo done!', result)

  // But the best part is, we can just keep awaiting different stuff, without ugly .then()s
  var somethingElse = await getSomethingElse()
  var moreThings = await getMoreThings()
})()

Await is supported in all current browsers and node

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.