promise

Wrapping promise in async/await

巧了我就是萌 提交于 2021-02-07 03:20:40
问题 I'm struggling a bit with async/await and returning a value from a Promise. function test () { return new Promise((resolve, reject) => { resolve('Hello') }) } async function c() { await test() } As I understood things I should be able to get a value by doing: console.log(c()) But clearly I am missing a point here as this returns a promise. Shouldn't it print "hello"? On a similar note I am unclear as to whether a callback needs to be converted to a promise before wrapping it in async/await?

js - How to call an async function within a Promise .then()

给你一囗甜甜゛ 提交于 2021-02-06 22:57:43
问题 First, I have to mention that I already look through many questions in stackoverflow, but many doesn't answer my question. Not to mention many doesn't even have an answer. How do I achieve the following, making sure functionB() executes after functionA() finishes? Note: I do not want to convert my async functions to new Promise(resolve=>{...}) because I'll have to convert the someServiceThatMakesHTTPCall() as well, and any other async functions within the call stack, which is a big change.

js - How to call an async function within a Promise .then()

隐身守侯 提交于 2021-02-06 22:37:27
问题 First, I have to mention that I already look through many questions in stackoverflow, but many doesn't answer my question. Not to mention many doesn't even have an answer. How do I achieve the following, making sure functionB() executes after functionA() finishes? Note: I do not want to convert my async functions to new Promise(resolve=>{...}) because I'll have to convert the someServiceThatMakesHTTPCall() as well, and any other async functions within the call stack, which is a big change.

Why does my function execute before my promise callback?

笑着哭i 提交于 2021-02-05 11:27:07
问题 Why does a function called after my promise execute before the promise's callback? I read this in MDN, but didn't understand it "Callbacks will never be called before the completion of the current run of the JavaScript event loop." I thought it meant that if I have any other statements after resolve() or reject() they will get executed before the callback is invoked. Though, that seems to be an incomplete understanding. function myFunction() { return new Promise( function(resolve, reject) {

Using async/await with a forEach loop

那年仲夏 提交于 2021-02-05 09:39:39
问题 Are there any issues with using async / await in a forEach loop? I'm trying to loop through an array of files and await on the contents of each file. import fs from 'fs-promise' async function printFiles () { const files = await getFilePaths() // Assume this works fine files.forEach(async (file) => { const contents = await fs.readFile(file, 'utf8') console.log(contents) }) } printFiles() This code does work, but could something go wrong with this? I had someone tell me that you're not

How to avoid nesting structure of callbacks with promises? [finished]

半城伤御伤魂 提交于 2021-02-05 08:59:05
问题 I am using promises to avoid the nesting structure created by callbacks. However in this code I still have some nesting. Is there something I am doing wrong or is this un-avoidable in this case? In this case I want to check and see if a profile exists and if it does not I want to create it. DB.getProfile(id_google).then((resGet) => { if(!resGet[0]){ console.log('PROFILE - NOT FOUND - MUST CREATE'); DB.createProfile(id_google, email, name, pic_url).then((resCreate)=>{ console.log('PROFILE

Why is .json() asynchronous? [duplicate]

末鹿安然 提交于 2021-02-05 05:52:28
问题 This question already has answers here : Why does .json() return a promise? (4 answers) Closed last year . I've been following a tutorial and came across the following code snippet: const myAsyncFunction = async () => { const usersResponse = await fetch( 'https://jsonplaceholder.typicode.com/users' ) const userJson = await usersResponse.json(); const secondUser = userJson[1]; console.log(secondUser); const posts = await fetch ( 'https://jsonplaceholder.typicode.com/posts?userId=' + secondUser

Intercept XMLHttpRequest and send a Promise

谁说胖子不能爱 提交于 2021-02-04 21:48:40
问题 I am building a proxy for a video player which can intercept the http requests and send back the response using a Promise which can be resolved asynchronously I have found this way of intercepting a http request // save original `send` method const origSend = XMLHttpRequest.prototype.send; // redefine `send` method // you could also pass extra parameters if needed XMLHttpRequest.prototype.send = (...origParams) => { console.log('send called'); origSend(...origParams); } Now I don't want to

Intercept XMLHttpRequest and send a Promise

五迷三道 提交于 2021-02-04 21:44:47
问题 I am building a proxy for a video player which can intercept the http requests and send back the response using a Promise which can be resolved asynchronously I have found this way of intercepting a http request // save original `send` method const origSend = XMLHttpRequest.prototype.send; // redefine `send` method // you could also pass extra parameters if needed XMLHttpRequest.prototype.send = (...origParams) => { console.log('send called'); origSend(...origParams); } Now I don't want to

Promise - `then()` not works as expect

一世执手 提交于 2021-02-04 19:43:06
问题 I do have 2 function. I am chaining with then() method for promise . But I am trying to initiate the second function after the first promise happend. But now the second function call as first. how to fix this? or any issue with my code? here is my try: var getData = function(){ return new Promise((resolve, reject) => { setTimeout(() => { resolve(42); //consoles as second }, 5000); }) } var getDataMoreData = function(){ return new Promise((resolve, reject) => { setTimeout(() => { resolve(43);