Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How make promise execute synchronously?

I use dom-to-image.js for converting dom to png image. As dom-to-image.js uses promise, the code executes asynchronously. I want to execute .then function synchronously.

I have the following code:

domtoimage.toPng(document.getElementById("main")).then(function(dataUrl) {
    console.log(dataUrl);
}).catch(function(error) {
    console.error('oops, something went wrong!', error);
});

console.log("this console should be executed after console.log(dataUrl)")

I want to execute .then function first, before executing console.log("this console should be executed after console.log(dataUrl)").

Please tell me some way to achieve this.

like image 224
Jeffrin John Avatar asked Sep 18 '17 07:09

Jeffrin John


People also ask

Is promise asynchronous or synchronous?

In JavaScript, promises are special objects that help you perform asynchronous operations. You can create a promise using the Promise constructor. You need to pass an executor function to it. In the executor function, you define what you want to do when a promise returns successfully or when it throws an error.

How do you resolve a promise in a synchronous function?

Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.

How do you make a promise then synchronous?

If you're not concerned with IE support, you could use async and await to execute the promise as though it were synchronous. The await syntax will pause execution of the function until the promise resolves, letting you write the code as if there wasn't a promise. To catch an error, you wrap it in a try/catch block.

Does promise execute synchronously?

The function Promise. all runs in parallel. This makes it fast. However, there are situations where you want it to run synchronously, one after the other.


2 Answers

There are of course legit reasons to force synchronous execution of a promise in js. The most obvious is when require'ing a file that needs to initialize using some asynchronous stuff, and optimization is not an overriding concern.

Seems like the package synchronized-promise seems like it ought to do the trick. In your case (warning - untested..):

const dti = () => docstoimage.toPng(document.getElementById("main"))
  .then(dataUrl => console.log('url: ', dataUrl))
  .catch(err => console.error('oops: ', err))

const sp = require('synchronized-promise')
const syncDti = sp(dti)
syncDti() // performs the 'synchronized version'

console.log("this console should be executed afterwards")
like image 93
fresidue Avatar answered Oct 06 '22 20:10

fresidue


Try this:

async function doStuff(arg) {console.log(arg + "Assume this is a useful async function")}


doStuff.apply(/* What to specify as this */ this, /* List of args */ ["hello world "])
like image 32
Shaurya Chhabra Avatar answered Oct 06 '22 21:10

Shaurya Chhabra