promise

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

笑着哭i 提交于 2021-02-10 12:18: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

Node.js readline inside of promises

♀尐吖头ヾ 提交于 2021-02-10 12:16:06
问题 I'm trying to use the node.js package readline to get user input on the command line, and I want to pipe the entered input through promises. However, the input never gets through the then chain. I think the problem could come from the fact that the promises are fulfilled in the callback method, but I don't know how to solve that problem. An example of this problem looks like this: import rlp = require('readline'); const rl = rlp.createInterface({ input: process.stdin, output: process.stdout }

How await a recursive Promise in Javascript

廉价感情. 提交于 2021-02-10 06:54:18
问题 I have written a recursive Promise in javascript which seems to be working fine but I wanted to test it using setTimeout() to be sure that I'm awaiting correctly before continuing with the execution. Here is the gist of my code: try{ await renameFiles(); // <-- await here console.log("do other stuff"); } catch(){ } const renameFiles = (path) => { return new Promise(resolve => { console.log("Renaming files..."); fs.readdirSync(path).forEach(file) => { // if file is a directory ... let newPath

Cannot read property `.then` of undefined with axios and react in actions

心不动则不痛 提交于 2021-02-10 06:18:19
问题 I am using axios in an action and trying to call that action with a chaining action. I will show what I am trying to do here: this.props.fetchOffers().then(() => { this.props.filter(this.props.filterOption); }); But I get an error: Cannot read property 'then' of undefined . What I do not get is that right below this function I have another action that is doing this exact same thing and working just fine. this.props.sortOffers(value).then(() => { this.props.filter(this.props.filterOption); });

Cannot read property `.then` of undefined with axios and react in actions

非 Y 不嫁゛ 提交于 2021-02-10 06:17:33
问题 I am using axios in an action and trying to call that action with a chaining action. I will show what I am trying to do here: this.props.fetchOffers().then(() => { this.props.filter(this.props.filterOption); }); But I get an error: Cannot read property 'then' of undefined . What I do not get is that right below this function I have another action that is doing this exact same thing and working just fine. this.props.sortOffers(value).then(() => { this.props.filter(this.props.filterOption); });

Handling Redux Saga result in view from which action call originates

微笑、不失礼 提交于 2021-02-09 10:57:33
问题 I'm new to Redux Saga, coming from Redux Thunk. In some situations, I need to know whether an API call fails or succeeds from inside the view from which I called the action. With Redux Thunk, I would do something like the following. My component and action creator would look like this: class MyComponent extends Component { componentDidMount() { this.props.actions.fetchItems() .then(result => { if (result.status === 'OK') { console.log('Request succeeded, take particular action in this view')

Handling Redux Saga result in view from which action call originates

喜你入骨 提交于 2021-02-09 10:57:15
问题 I'm new to Redux Saga, coming from Redux Thunk. In some situations, I need to know whether an API call fails or succeeds from inside the view from which I called the action. With Redux Thunk, I would do something like the following. My component and action creator would look like this: class MyComponent extends Component { componentDidMount() { this.props.actions.fetchItems() .then(result => { if (result.status === 'OK') { console.log('Request succeeded, take particular action in this view')

How can I call a function after grecaptcha.execute() has finished executing - triggered by an event?

☆樱花仙子☆ 提交于 2021-02-08 14:58:13
问题 Currently grecaptcha.execute is being executed on page load as in the first JS example below. If reCAPTCHA challenge is triggered this happens when the page has loaded. Ideally this would happen when the form submit button is clicked instead. So I've tried this by moving this into the submit event (second JS example) and put the axios function into a promise. It's submitting before grecaptcha.execute has finished executing. What is it that I'm not understanding here? My first experience with

JS Promises: is doing `return(value)` in a `then` block the same as resolving?

北城余情 提交于 2021-02-08 11:58:17
问题 I have the following code: new Promise((resolve, reject) => { resolve(1) }).then(value => { return 2 }) I resolve the initial promise with 1 . Then in the then block I do return 2 . Does this return a promise resolved with the value 2? 回答1: Yes. Calling .then creates a new promise, and that promise will resolve to whatever you return in the callback. 来源: https://stackoverflow.com/questions/61510453/js-promises-is-doing-returnvalue-in-a-then-block-the-same-as-resolving

Return when first promise resolves

為{幸葍}努か 提交于 2021-02-08 11:33:56
问题 Goal I have a bunch of file names in an array, and would like to read the contents of the first of the files that exists. They're config files, so it's important that the order is deterministic, so I can't use .race() . The version I have below maps over each file in order, tries to load it, and if it loads successfully, calls resolve. Problems Here are a couple of issues with this implementation: Calling resolve(...) doesn't actually exit the loop, so the program opens every file in the list