promise

async is a promise function and with setimeout how it works [duplicate]

江枫思渺然 提交于 2020-07-03 12:56:31
问题 This question already has answers here : Combination of async function + await + setTimeout (10 answers) Is using async in setTimeout valid? (2 answers) Closed 17 days ago . const name = async () => { setTimeout(() => { return Promise.resolve("1") },1000); }; (async function () { console.log("hello2"); })(); const timer = async () => { console.log(await name()); console.log("hello4"); }; timer() expected output hello2 1 hello4 output is hello2 undefined hello4 If i removed the settimeout

JS AJAX: promise dependant submit always submitted

守給你的承諾、 提交于 2020-06-29 04:29:40
问题 Finally, I managed to do this: function validar(dato_a_validar, url, secc) { var datastr = "secc=" + secc + "&dato=" + dato_a_validar; return new Promise(function(resolve, reject) { $.ajax({ type:'GET', url:url, data:datastr, success:function(response) { resolve(response); }, error:function(response) { reject(response); } }); }); } async function check_submit() { init(); // sets some global variables errores_personal_cnt = 0; // error counter var dni_ctl = document.getElementById("frm

When NOT to use promises [closed]

梦想的初衷 提交于 2020-06-24 05:09:13
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 years ago . Improve this question After having read dozens of articles on how great es6 promises are and why we should implement them I have been left with the feeling that ALL of my (non-trivial) javascript functions should be promises. In fact I feel great when writing code using them

Returning await'd value in async function is returning a promise

允我心安 提交于 2020-06-23 11:02:44
问题 In Javascript, I am trying to return an await'd result from an async function. It seems if I use that result inside the async function then everything works fine, it gets treated as the resolve() parameter and everything is fine and dandy. However if I try to return the result, it gets treated as a callback, even though the await statement is there. For example (using await'd result inside the async func): https://jsfiddle.net/w7n8f7m7/ <script src="https://ajax.googleapis.com/ajax/libs

Returning await'd value in async function is returning a promise

蹲街弑〆低调 提交于 2020-06-23 11:01:20
问题 In Javascript, I am trying to return an await'd result from an async function. It seems if I use that result inside the async function then everything works fine, it gets treated as the resolve() parameter and everything is fine and dandy. However if I try to return the result, it gets treated as a callback, even though the await statement is there. For example (using await'd result inside the async func): https://jsfiddle.net/w7n8f7m7/ <script src="https://ajax.googleapis.com/ajax/libs

different promise execution on browsers, what happened?

我只是一个虾纸丫 提交于 2020-06-23 04:51:29
问题 const b = () => { return new Promise(resolve => { resolve(); Promise.resolve() .then(() => { console.log(1); }) .then(() => console.log(3)); }); }; const a = async () => { await b(); console.log(2); }; a(); Different behavior on safari , chrome(firefox), any standard described about this ? 回答1: You have two completely independent promise chains here: Promise.resolve() .then(() => { console.log(1); }) .then(() => console.log(3)); (async () => { await new Promise(resolve => { resolve(); });

How do I use promises in a Chrome extension?

陌路散爱 提交于 2020-06-22 02:59:07
问题 What I am trying to do is create a chrome extension that creates new, nested, bookmark folders, using promises. The function to do this is chrome.bookmarks.create() . However I cannot just loop this function, because chrome.bookmarks.create is asynchronous. I need to wait until the folder is created, and get its new ID, before going on to its children. Promises seem to be the way to go. Unfortunately I cannot find a minimal working example using an asynchronous call with its own callback like

Catching an exception from the setTimeout function executed inside an instance of a Promise object [duplicate]

独自空忆成欢 提交于 2020-06-21 05:37:07
问题 This question already has answers here : Asynchronous exception handling with bluebird promises (3 answers) Closed 5 days ago . Dear participants please tell me the solution. In this block of code, the catсh method perfectly catches the exception: const myPromise = new Promise(() => { throw new Error(`Oops! Threw an exception`); }); // We catch the exception in the method `catch`. myPromise .catch((error) => console.log(error.message)); And in this block, the catсh method will not be called:

ES6 Arrow Functions and Promise Chaining condensed syntax explanation

十年热恋 提交于 2020-06-17 15:53:31
问题 In the following code block, can someone please provide links or an explanation for the condensed alert statement syntax. I understand the preceding expanded equivalent code that is commented out and contains the message parameter. However, I cannot find a reference to the syntax for omitting the message parameter: let timeoutPromise = new Promise((resolve, reject) => { setTimeout(() => { resolve('Success!'); }, 2000); }); /* timeoutPromise.then(message => { alert(message); }) */

Reducing nested promises

吃可爱长大的小学妹 提交于 2020-06-17 09:34:10
问题 I am reducing an array of nested promises in the following way: const promises = items.map((item, index) => { console.log(index); return doAsyncThing(item).then(() => { console.log(index); }); }); return promises.reduce((acc, p) => acc.then(() => p), Promise.resolve()); I want the console.log entries to print out 0 0 1 1 2 2 but instead they are printing out 0 1 2 2 1 0 How can I restructure my code so that the entirety of operation 0 is completed before operation 1, which is completed before