Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to understand this Promise code?

'use strict';

Promise.resolve(() => 'John')
  .then((args) => {
    console.log(args);
    throw new Error('ops')
  })
  .catch((ex) => {
    console.log(ex)
  })
  .then(() => {
    throw new Error('ups')
    console.log('Doe')
  })

I think console.log(args); should output 'John', but when I run this code, the output is [ [Function] ]

So I am confused.

like image 795
BlackMamba Avatar asked Jan 08 '16 01:01

BlackMamba


People also ask

What is promise code?

A Promise is a JavaScript object that links producing code and consuming code.

How does a promise work?

It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.

How do you consume a promise?

Consuming a Promise We consume a promise by calling then() and catch() methods on the promise. For example, requesting data from an API using fetch which returns a promise. The successCallback is called when a promise is resolved. It takes one argument which is the value passed to resolve() .

How do you handle a returned promise?

load must return a Promise or thenable . The first call will initiate the async methods. Further calls need to return a Promise which can only resolve or reject AFTER the initial Promise has completed. Chaining the promises will only work if no values are passed to the success handler (The first .

What is the result of a JavaScript promise?

"I Promise a Result!" A JavaScript Promise object contains both the producing code and calls to the consuming code: When the executing code obtains the result, it should call one of the two callbacks: The Promise object supports two properties: state and result. While a Promise object is "pending" (working), the result is undefined.

What is the difference between a promise and a promise object?

While a Promise object is "pending" (working), the result is undefined. When a Promise object is "fulfilled", the result is a value. When a Promise object is "rejected", the result is an error object. You cannot access the Promise properties state and result. You must use a Promise method to handle promises.

Is it possible to implement a single Promise?

Implementing a single Promise is pretty straightforward. In contrast, Chained Promises or the creation of a dependency pattern may produce “spaghetti code”. The following examples assume that the request-promise library is available as rp.

How to resolve a promise using await in JavaScript?

The function that encompasses the await declaration must include the async operator. This will tell the JS interpreter that it must wait until the Promise is resolved or rejected. The await operator must be inline, during the const declaration. This works for reject as well as resolve. Implementing a single Promise is pretty straightforward.


2 Answers

Promise.resolve will create a new Promise resolved with the value you pass to it. So, in your case, your promise is actually resolved with the function object. It means that, the then handler is passed the function object itself.

What you should have done is

new Promise((resolve, reject) => resolve('John'))
  .then(args => {
    console.log(args);
    throw new Error('ops')
  })
  .catch(console.log.bind(console));

Now, you are creating a Promise object and you are resolving that with the value John.


If you want your Promise to be resolved with a value readily, then don't pass the function object but pass the actual value itself to Promise.resolve function.

Promise.resolve('John')
  .then(args => {
    console.log(args);
    throw new Error('ops')
  })
  .catch(console.log.bind(console));

Now, you have a Promise, resolved with value John and the then handler will get the resolved value John.

Note: This is the recommended way of creating a Promise when you know the actual way to resolve with readily, so that you can avoid the Promise constructor anti-pattern.

like image 57
thefourtheye Avatar answered Oct 18 '22 12:10

thefourtheye


'use strict';

Promise.resolve('John')
  .then((args) => {
    console.log(args);
    throw new Error('ops')
  })
  .catch((ex) => {
    console.log(ex)
  })
  .then(() => {
    throw new Error('ups')
    console.log('Doe')
  })

I modify Promise.resolve('John'), it works. Please see the Promise.resolve.

like image 24
BlackMamba Avatar answered Oct 18 '22 14:10

BlackMamba