← Back to challenges

Promises IX: Composition Introduction

JavaScriptHardfunctional_programming

Instructions

The built-in javascript Promise object has several methods you can use to compose your asynchronous operations. The Promise.resolve() function takes either a value or a promise.

var original = Promise.resolve(33)
var cast = Promise.resolve(original)
cast.then(function(value) {
  console.log('value: ' + value)
})

So what is going on here?

  1. original is assigned a promise that is passed the value 33.
  2. cast is passed the promise original.
  3. The then function is passed original and becomes the then function for original.
  4. Then it takes original's value(33) and logs it to the console.

One other thing Promise.resolve() does is that it will cast anything that might be a promise(ie. thenables) to a native Javascript Promise. This is generally the use case for it.

Use a Promise method to turn the thenable object into a native promise and assign it to the promise variable. Assign the fulfilled value to the result variable.

Notes

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.