There must be synchronous code called by promise. Then create a new promise

I have implemented some code in which asynchronous code is followed by some synchronous functions. For instance:

function processSomeAsyncData() { asyncFuncCall() .then(syncFunction) .catch(error); } 

If I understand correctly, then this is also a promise. Then should I create a promise in synchronous code too?

 function syncFunction() { const p = new Promise (function (resolve, reject) { //Do some sync stuff ... resolve(data); } return p; } 

If this is not necessary, how to reject a promise from synchronous code if an error occurs?

+6
source share
3 answers

You do not need to explicitly create a new promise. There is an easier way.

This example is contrived because it will never fail, but the point is that you do not need to create a promise and you do not need to return a solution (val).

 function syncFunction() { var j = "hi" if(j){ return j; } return new Error('i am an error'); } 

This will work:

 asyncFunction() .then(syncFunction); 

But if you did it the other way around:

 syncFunction() .then(asyncFunction); 

You need to define your synchronization as:

 function syncFunction() { var j = "hi" return new Promise((resolve, reject) => { if(j){ return resolve(j); } return reject('error'); }) } 

Edit: To prove to everyone who doesn't believe you, let this guy shoot locally on your computer. It is proven that you have many options for you. :)

 var Promise = require('bluebird'); function b(h) { if(h){ return h; } return Promise.resolve('hello from b'); } function a(z) { return new Promise((resolve, reject)=> { if(z){return resolve(z)}; return resolve('hello from a'); }) } a().then(b).then(x => console.log(x)).catch(e => console.log(e)); b().then(a).then(x => console.log(x)).catch(e => console.log(e)); 
+6
source

It's not obligatory.

If you return the promise from syncFunction , your original promise will be resolved only after the new promise is resolved, and any value returned by the new promise will be transferred to the next then in the chain.

If you return a value without promises, which will be passed to the next then in the chain.

Reject inside syncFunction , just throw an exception.

+3
source

No. Synchronous functions can be called from synchronous code and must always be interrupted synchronously! They should not correspond to asynchronous subscribers. If an error occurs, just throw an error. Try:

 var asyncFuncCall = () => Promise.resolve(); function syncFunction() { throw new Error("Fail"); } asyncFuncCall() .then(syncFunction) .catch(e => console.log("Caught: " + e.message)); 

This works because the exception thrown by the function passed in .then translates into rejection of the promise that it should return.

In addition, any value returned by a function passed in .then is converted to a promise resolved with that value. The promise code calling the function will take care of this.

This allows you to easily combine synchronous and asynchronous code:

 asyncFuncCallOne() .then(() => { var x = syncFunction(); return asyncFuncCallTwo(x); }) .catch(e => console.log(e.message)); 
+2
source

All Articles