【发布时间】:2021-11-11 20:05:32
【问题描述】:
我使用 dom-to-image.js 将 dom 转换为 png 图像。由于 dom-to-image.js 使用 Promise,因此代码异步执行。我想同步执行 .then 函数。
我有以下代码:
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)")
我想先执行.then函数,然后再执行console.log("this console should be executed after console.log(dataUrl)")。
请告诉我一些实现这一目标的方法。
【问题讨论】:
-
为什么不放在then里面呢?还是做链接?
-
这看起来像an XY problem。相信我,您不想要同步执行 Promise,因为 1. 这是不可能的,并且 2. 它会阻碍性能。您实际上想在这里实现什么?
-
@PatrickRoberts 有有效的用例。如果你有一个遗留的、同步的代码库与一个异步的集成,你可能希望同步的东西异步和异步的东西同步到原型。
标签: javascript promise es6-promise