promise

await for jQuery.when ajax requests

妖精的绣舞 提交于 2021-02-19 09:41:23
问题 This code inside async function, does not give the expected result: var result = await $.when( $.get('/api/1'), $.get('/api/2') ); with one request, result will be the output I expect (the response text). However, with these two requests, the returned result is an array which does not hold the two Promises values. Is there any workaround? I know there are then() and done() , but I prefer using await. 回答1: jQuery's .when() and the native await have different semantics. Compare: // jQuery $

jQuery sequential functions one after the other in a each loop

你。 提交于 2021-02-19 08:47:06
问题 I know this topic has been discussed in a couple of posts but I can't seem to get my head around doing this for my particular use case. So I have a need to simulate the typing of values into inputs on a page one after the other. The user simply sits and watches - but essentially what I need is on load, the first input focuses > types in letter by letter a value, and then moves to the next input to do the same with it's value. Here is a fiddle of what I mean with my code so far: https:/

bootstrap typeahead not populating with response from promise

[亡魂溺海] 提交于 2021-02-19 06:24:10
问题 My bootstrap typeahead is as following: <input id="inputId" type="text" ng-model="selected" typeahead="name for name in getSuggestions($viewValue) " typeahead-on-select="typeaheadOnSelect($item)" ng-trim="false"> The getSuggestions() return a promise,like following: $scope.getSuggestions = viewValue => { let deferred = $q.defer(); getSuggestions(viewValue).then(words => { deferred.resolve(words); // array of strings }) .catch(()=>{ deferred.reject([]); }); return deferred.promise; }; NOTE :

About Node.js Promise then and return?

穿精又带淫゛_ 提交于 2021-02-19 06:10:42
问题 I'm confused about Promise! I use Promise then without return like this: new Promise((resolve, reject) => { resolve("1"); }).then((v1) => { console.log("v1"); new Promise((resolve, reject) => { //Time-consuming operation, for example: get data from database; setTimeout(() => { resolve(2) }, 3000); }).then((v11) => { console.log("v11"); }) }).then((v2) => { console.log("v2") }); I get this result v1 v2 v11 . Then, I use another way of writing, Like below: new Promise((resolve, reject) => {

Why 'await' requires 'async' in function definition

主宰稳场 提交于 2021-02-19 05:56:09
问题 I'm currently learning Dart, but this is also applicable to what's going on in the JavaScript world right now, and it seems like C# also uses the same pattern. In Dart, any function that uses await must itself be labeled asynchronous through async as follows: import "dart:html"; main() async { var context = querySelector("canvas").context2D; var running = true; while (running) { var time = await window.animationFrame; ... } } This does not make sense to me. If a function is waiting on an

Why 'await' requires 'async' in function definition

∥☆過路亽.° 提交于 2021-02-19 05:56:08
问题 I'm currently learning Dart, but this is also applicable to what's going on in the JavaScript world right now, and it seems like C# also uses the same pattern. In Dart, any function that uses await must itself be labeled asynchronous through async as follows: import "dart:html"; main() async { var context = querySelector("canvas").context2D; var running = true; while (running) { var time = await window.animationFrame; ... } } This does not make sense to me. If a function is waiting on an

Why do promises execute at the point of declaration?

我是研究僧i 提交于 2021-02-19 03:11:30
问题 I would like to execute a set of promises using Promise.all(). My approach is to put these promises in an array and then pass the array to Promise.all(). However, I find that the promises start executing as soon as they are declared and do not even wait for Promise.all to be called. Why is this happening and how can I have the promises only execute upon calling Promise.all()? let promiseArray = []; const values = [1, 2, 3, 4, 5]; values.forEach((value)=>{ promiseArray.push( new Promise(

Why do promises execute at the point of declaration?

放肆的年华 提交于 2021-02-19 03:11:15
问题 I would like to execute a set of promises using Promise.all(). My approach is to put these promises in an array and then pass the array to Promise.all(). However, I find that the promises start executing as soon as they are declared and do not even wait for Promise.all to be called. Why is this happening and how can I have the promises only execute upon calling Promise.all()? let promiseArray = []; const values = [1, 2, 3, 4, 5]; values.forEach((value)=>{ promiseArray.push( new Promise(

How does Promise run when .then method is not called?

巧了我就是萌 提交于 2021-02-18 20:51:10
问题 I create two promises, but I do not run the then method on those promises. Yet once the promise objects go out of scope, the promise code runs as if .then was called. How is a Promise settled without a call to the .then method? I am asking because I would like to load an array with Promise objects and then run the promises in sequence. function promises_createThenRun() { const p1 = createPromise1(); const p2 = createPromise2(); console.log('before hello'); alert('hello'); console.log('after

Correct mental model for a Javascript async function's 'await': generator's 'yield' vs. 'promise.then()'?

早过忘川 提交于 2021-02-18 07:43:12
问题 Which of a generator's yield vs. promise.then() is a more* correct mental model for understanding 'await'? Property comparison, inferred by stepping through the snippet below with a debugger: await: await does not pause/suspend the running async function’s execution. (The running async function ‘runs to completion’, returning a pending promise when the interpreter hits the 1st await. It’s then immediately removed from the call stack.) await waits for the promise to settle. await expression