promise

Execute promises map sequentially

北城以北 提交于 2020-05-14 20:07:42
问题 I have written a function that is being called in a loop (map) and that function is using promises. Now, I want that function to run synchronously and exit before its next instance is called. function t1(){ let arr1 = [1,2,3,4,5]; return Promise.map(arr1, (val) =>{ const params = { "param1" : val1 }; return t2(params); }); } function t2(event){ return Promise.resolve() .then({ //do something //code doesn't reach here in sync manner. all five instance are invoked and then code reaches here for

Javascript, Promises and setTimeout

╄→гoц情女王★ 提交于 2020-05-13 07:34:52
问题 I have been playing with Promises, but I am having trouble understanding what is happening with the following code: const promise = new Promise((resolve, reject) => { console.log('Promise started - Async code started') setTimeout(() => { resolve('Success') }, 10) }) setTimeout(() => { console.log('Promise log inside first setTimeout') }, 0) promise.then(res => { console.log('Promise log after fulfilled') }) console.log('Promise made - Sync code terminated') setTimeout(() => { console.log(

How make promise execute synchronously?

独自空忆成欢 提交于 2020-05-11 04:58:42
问题 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,

How make promise execute synchronously?

余生长醉 提交于 2020-05-11 04:57:07
问题 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,

Callback 与 Promise 间的桥梁 —— promisify

…衆ロ難τιáo~ 提交于 2020-05-05 12:54:24
作者:晃晃 本文原创,转载请注明作者及出处 Promise 自问世以来,得到了大量的应用,简直是 javascript 中的神器。它很好地解决了异步方法的回调地狱、提供了我们在异步方法中使用 return 的能力,并将 callback 的调用纳入了自己的管理,而不是交给异步函数后我们就无能为力了(经常有 callback 被莫名调用两次而导致程序出错)。 今天要介绍的是 Promisify,就是回调函数与 Promise 间的桥梁。 1. promisify 介绍 什么是 promisify 呢?顾名思义,就是“promise 化”,将一个不是promise的方法变成 promise 。举个例子: // 原有的callback调用 fs.readFile('test.js', function(err, data) { if (!err) { console.log(data); } else { console.log(err); } }); // promisify后 var readFileAsync = promisify(fs.readFile); readFileAsync('test.js').then(data => { console.log(data); }, err => { console.log(err); }); 这两个方法效果上是等价的

Authorization header not being sent when using fetch

允我心安 提交于 2020-04-30 07:49:59
问题 When I try and set the Authorzation header as below the header doesn't get sent to the server for the request. What's the correct way to set the Authorization header with fetch? let options = { method: 'GET', headers: new Headers({ Authorization: 'Bearer ...' }) }; fetch('/api/somedata', options).then(function(response) { console.log(response); }; Edit In chrome developer tools on the network tab I get this for the request: GET /api/somedata HTTP/1.1 Host: someserver.azurewebsites.net

How are javascript promises handled in the runtime environment

帅比萌擦擦* 提交于 2020-04-30 07:01:25
问题 I'm trying to figure out how promises are handled in the runtime environment. Are they moved into the web API container until they resolve and then pushed into the callstack when .then is called? Here is some example code. Console.log runs before the promises which leads me to believe somewhere along the way they end up in the queue. I also noticed I can put a function in a .then and the returned promise will fill that functions parameters. // asynchronous test let promiseWhatever = new

How are javascript promises handled in the runtime environment

给你一囗甜甜゛ 提交于 2020-04-30 07:00:08
问题 I'm trying to figure out how promises are handled in the runtime environment. Are they moved into the web API container until they resolve and then pushed into the callstack when .then is called? Here is some example code. Console.log runs before the promises which leads me to believe somewhere along the way they end up in the queue. I also noticed I can put a function in a .then and the returned promise will fill that functions parameters. // asynchronous test let promiseWhatever = new

waiting for web worker making http call to finish

时光总嘲笑我的痴心妄想 提交于 2020-04-30 06:39:54
问题 I'm creating multiple web workers making http calls. I have to limit the number of web workers and so I'm trying to wait for some of the workers to finish. here's an example of what I thought might work using Promises: anArray.map(async contact => { await new Promise((res, rej) => { const worker = new Worker('./http.worker', { type: 'module' }); worker.onmessage = () => { res(); }; worker.postMessage(contact); }); }); I thought this would wait for each promise to resolve before moving on to

Angular 8 why the promise is returning an undefined value despite that it's properly consoled just before the resolve?

纵饮孤独 提交于 2020-04-18 07:25:17
问题 EDIT I consoled if the script is entering the addHousehold() if condition at: addHouseholdPromise.then((res) => { console.log("Promise HH: "+res) if (res != "add_hh_fail") { console.log("success: "+res) return res; } else { console.log("fail: "+res) // Add to addHousehold array log this.addHHArray(row) } }) And it returned the value, but when I intercept it at the main function, it becomes undefined . END EDIT Original Post I have a main function, which loop over an array fromArray , and it