promise

JS Promises: if a handler in a `then` block returns a value vs returning a resolved promise, does the `then` block handle it the same way?

﹥>﹥吖頭↗ 提交于 2021-02-11 14:01:05
问题 Say I have a function that returns a resolved promise like this: let a = () => {return new Promise(res => res(1))} and then I then-ify it like this: a() .then(val => {return new Promise(res => res(1))}) Here the then contains a handler that returns a promise resolved with 1 , so the then block returns a promise resolved with 1 as well. Is that right? Then say instead we have this: a() .then(val => {return 1}) The handler returns 1 instead of returning a promise resolved with 1 . What I Want

I want to take out the result from mysql's connection.query and save it in global scope chain in nodejs

非 Y 不嫁゛ 提交于 2021-02-11 13:02:14
问题 I tried bringing out result by storing in variable current product. But I cant use it outside the function, so my array returns empty var connection = mysql.createConnection({ host: config.config.mysql.opencart_local.host, user: config.config.mysql.opencart_local.user, password: config.config.mysql.opencart_local.password, database: config.config.mysql.opencart_local.database }) var query_current_products = 'select * from table;'; var current_products = []; connection.connect(function(err) {

Wait for nested JS promise to finish before resolving original promise

倾然丶 夕夏残阳落幕 提交于 2021-02-11 12:27:38
问题 I am new to Promises and I'm having a little trouble with the concept of waiting for a nested promise to finish all executions before resolving the original promise. Original Code function getSomething(text) { return new Promise(function (resolve, reject) { getElse(text).then(function (items) { if (items.length !== 0) { /* Stuff here */ getElseElse(box).then(function (moreItems) { /* Stuff here */ return array; }.then(function (array) { var promise = new Promise(function (resolve, reject) { /

Wait for nested JS promise to finish before resolving original promise

我们两清 提交于 2021-02-11 12:25:07
问题 I am new to Promises and I'm having a little trouble with the concept of waiting for a nested promise to finish all executions before resolving the original promise. Original Code function getSomething(text) { return new Promise(function (resolve, reject) { getElse(text).then(function (items) { if (items.length !== 0) { /* Stuff here */ getElseElse(box).then(function (moreItems) { /* Stuff here */ return array; }.then(function (array) { var promise = new Promise(function (resolve, reject) { /

Using async/await with a forEach loop

假装没事ソ 提交于 2021-02-11 12:24:29
问题 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 do nested Javascript returns “pass up” promises?

天大地大妈咪最大 提交于 2021-02-11 12:23:14
问题 This question has two parts, both relating to how Javascript promises are passed around functions using return statements. 1) I have simple Javascript function, which includes multiple return statements. The inner function returns a promise to an arrow function, the arrow function is also returned, like this: const returnMe(data){ return () => { return Promise.resolve(data); }; }; Could I write the following code? returnMe("Hello!").then((msg) => { console.log(msg) }); /// --> output "Hello!"

What is a safe and scalable way to exhaustively select all users from Amazon Cognito API in JavaScript?

允我心安 提交于 2021-02-11 08:50:23
问题 I am part of a small team working on a fairly small website with user accounts; there are about 100 users at this time. And we are using Amazon Cognito for user management. On our website there is a summary page which displays a list/table of all users and various attributes. However, there's a hard limit on the number of items returned by the Amazon Cognito listUsers API call, in this case 60. Luckily, the API call also returns a token to use to make subsequent calls if there are more users.

javascript async/await and promise

我的梦境 提交于 2021-02-10 14:16:34
问题 I'm having a hard time understanding how async/await works.I have to make a program which contains three functions: func1 func2 and concatenated . func1 takes a string as an argument and returns the same string after 5 seconds of delay, func2 is an async function which also takes a string as an argument and returns the same string. concatenated is a function that takes two strings (s1,s2) as arguments and uses the above two functions( (func1(s1) and func2(s2)) ) to return their concatenated

javascript async/await and promise

 ̄綄美尐妖づ 提交于 2021-02-10 14:14:59
问题 I'm having a hard time understanding how async/await works.I have to make a program which contains three functions: func1 func2 and concatenated . func1 takes a string as an argument and returns the same string after 5 seconds of delay, func2 is an async function which also takes a string as an argument and returns the same string. concatenated is a function that takes two strings (s1,s2) as arguments and uses the above two functions( (func1(s1) and func2(s2)) ) to return their concatenated

Why does a Promise return an object Promise, even if I put `return` explicitly? [duplicate]

允我心安 提交于 2021-02-10 12:20:07
问题 This question already has answers here : JS: Promise doesn't return value (2 answers) Closed 1 year ago . I expected a Promise will return a value that I put. But It doesn't return a value but returns Promise { pending } var c = new Promise((resolve, reject)=>{ console.log('here'); return 'return here'; // resolve('resolve here') }); console.log(c); I expected there will be return here instead of Promise { pending } 回答1: Why does a Promise return an object Promise, even if I put return