es6-promise

Equivalent of BlueBird Promise.props for ES6 Promises?

匆匆过客 提交于 2019-12-22 04:49:19
问题 I would like to wait for a map of word to Promise to finish. BlueBird has Promise.props which accomplishes this, but is there a clean way to do this in regular javascript? I think I can make a new object which houses both the word and the Promise, get an array of Promises of those objects, and then call Promise.all and put them in the map, but it seems like overkill. 回答1: If you are dealing with a Map with values that are promises (or a mix of promises and non-promises) - and you want the

How to unit test Promise catch() method behavior with async/await in Jest?

南楼画角 提交于 2019-12-22 04:36:11
问题 Say I have this simple React component: class Greeting extends React.Component { constructor() { fetch("https://api.domain.com/getName") .then((response) => { return response.text(); }) .then((name) => { this.setState({ name: name }); }) .catch(() => { this.setState({ name: "<unknown>" }); }); } render() { return <h1>Hello, {this.state.name}</h1>; } } Given the answers below and bit more of research on the subject, I've come up with this final solution to test the resolve() case: test.only(

When should I call Promise.resolve() directly?

早过忘川 提交于 2019-12-22 04:20:59
问题 I've seen that the native ES6 Promise.resolve() can be invoked directly - as a static method. Facebook is using it that way in their Flux examples. But in what case should I do that? To queue something? Or instead of using window.setTimeout() ? 回答1: You should call Promise.resolve(object) when you need to create a promise that is already resolved. For example you may have a function that starts downloading a resource from the server or returns its cached version: function getImage(imageUrl) {

Typescript returning boolean after promise resolved

一笑奈何 提交于 2019-12-22 04:19:09
问题 I'm trying to return a boolean after a promise resolves but typescript gives an error saying A 'get' accessor must return a value. my code looks like. get tokenValid(): boolean { // Check if current time is past access token's expiration this.storage.get('expires_at').then((expiresAt) => { return Date.now() < expiresAt; }).catch((err) => { return false }); } This code is for Ionic 3 Application and the storage is Ionic Storage instance. 回答1: You can return a Promise that resolves to a boolean

Javascript : promise chain vs. async/await?

落爺英雄遲暮 提交于 2019-12-22 03:56:14
问题 I am learning about Javascript Promise and async / await . The sample code below asynchronously reads and parses a JSON file in node.js ( my node.js version is v10.0.0 ). In the sample code, ChainReadJson function and AwaitReadJson function are doing the same thing, reading and parsing a JSON file. The difference is that ChainReadJson function uses a promise chain, while AwaitReadJson function uses async/await. const FS = require("fs"); function ReadFile(fileName) { return new Promise(

Is it legitimate to omit the 'await' in some cases?

你说的曾经没有我的故事 提交于 2019-12-22 03:51:01
问题 I am using async / await in several places in my code. For example, if I have this function: async function func(x) { ... return y; } Then I always call it as follows: async function func2(x) { let y = await func(x); ... } I have noticed that in some cases, I can omit the await and the program will still run correctly, so I cannot quite figure out when I must use await and when I can drop it. I have concluded that it is "legitimate" to drop the await only directly within a return statement.

How to use jQuery's $.post() method with async/await and typescript

偶尔善良 提交于 2019-12-22 03:35:22
问题 My await statements inside the async functions are calls to jQuery's $.post() method which return a valid promise, however I am getting this error in TypeScript: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member. My function is this (simplified for the example). The code is valid and works, but I am getting a error in the TS console. async function doAsyncPost() { const postUrl = 'some/url/'; const postData = {name: 'foo', value: 'bar'}; let

Promise.resolve with no argument passed in

 ̄綄美尐妖づ 提交于 2019-12-21 09:19:23
问题 In the OpenUI5 code-base I came across this snippet: // Wait until everything is rendered (parent height!) before reading/updating sizes. // Use a promise to make sure // to be executed before timeouts may be executed. Promise.resolve().then(this._updateTableSizes.bind(this, true)); It looks like the native Promise function is being used, with no argument being passed to it's resolve function which takes an: Argument to be resolved by this Promise. Can also be a Promise or a thenable to

Awaited but never resolved/rejected promise memory usage [duplicate]

对着背影说爱祢 提交于 2019-12-21 06:47:16
问题 This question already has an answer here : Does never resolved promise cause memory leak? (1 answer) Closed last month . Will await ing a Promise which neither resolves nor rejects (never settle/unfulfilled) cause a memory leak? I got curious about this while looking at React hooks with slorber/awesome-debounce-promise that creates new promises, but only settles the last one of them, thus leaving many/most unsettle/unfulfilled. 回答1: Preface (you probably know this!): await is syntactic sugar

ES6 Promises/calling a function after multiple promises are fulfilled (can't use Promises.all) [duplicate]

浪尽此生 提交于 2019-12-21 05:25:28
问题 This question already has answers here : Wait until all promises complete even if some rejected (17 answers) Closed 3 years ago . I'm writing Javascript which needs these events to happen in this order: Fire off several API calls simultaneously Once all calls have completed and responses have returned, execute a line of code Sounds simple but the tricky part is that I can't use Promises.all() because I still want that line of code to execute after all promises have been fulfilled, successful