Promise | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Promise

I give up.🙌 Explain how it works: function foo(){ return new Promise((resolve,reject)=>{ let result = getSomeResult(); console.log(result); if (result) { resolve('Success'); console.log(resolve); } else { reject('Something went wrong'); console.log(reject()); } }); }

22nd Dec 2018, 9:23 AM
Solo
Solo - avatar
23 Answers
+ 7
Like this. Then is the resolve defined in promise, and variable s is the one passed from the promise catch is the reject foo().then(s=>{ console.log(s) //Success }).catch(s=>{ console.log(s) //Something went wrong })
22nd Dec 2018, 10:56 AM
Taste
Taste - avatar
+ 7
Lets see... We have a function called foo that will return a new Promise. So we'll create a new promise each time we call the function. And the promise is to get some result. And the promise is fulfilled if the result is not empty. Still isnt clear what you're asking here, can you be more specific on which part you dont understand?
22nd Dec 2018, 10:05 AM
Taste
Taste - avatar
+ 6
Vasiliy 1. Please be kind to //Zohir He is trying to help. 2. I understand you. Please don't panic. The following is the pre ES6 way of achieving this. https://code.sololearn.com/WkXxL9UPCZND/?ref=app You can see that it is actually nothing but a sophisticated way of doing something if some condition is matched and doing something else if the condition is not matched. The parameter passed to a Promise is what to determine and what to carry out.
22nd Dec 2018, 10:21 AM
Gordon
Gordon - avatar
22nd Dec 2018, 11:20 AM
Solo
Solo - avatar
+ 5
Gordon What you explain, I understand, but in this code: "function foo()", I can't figure out how to output the result.😕 https://code.sololearn.com/WAsY2t06nrIX/?ref=app
22nd Dec 2018, 10:42 AM
Solo
Solo - avatar
+ 4
// Zohir You have not been taught that speaking out about a person in the third person in his presence is indecent?
22nd Dec 2018, 10:13 AM
Solo
Solo - avatar
+ 4
Taste Thank you for the clarification. Could show an example how to create a new promise every time we call a function.
22nd Dec 2018, 10:26 AM
Solo
Solo - avatar
+ 4
Gordon ☺ Thank you! I easily disassembled your example, I meant the code "function foo()"
22nd Dec 2018, 11:58 AM
Solo
Solo - avatar
+ 3
Satnam Singh Maybe he want than we correct this code ?
22nd Dec 2018, 9:56 AM
program
program - avatar
+ 3
Vasiliy The function isGreater accepts a function cb as its third parameter, and execute it based on the result of comparing a and b. Because a<b, result is false, so the anonymous function prints smaller.
22nd Dec 2018, 11:22 AM
Gordon
Gordon - avatar
+ 3
Arjun Singhal Do not spam in SoloLearn Q&A forum. Considering this is your first offence, amd that you have said "sorry", you are given 24 hour to delete your spam; If I do not see your spam removed by yourself, I will log your behavior in moderator system. The delete button is, if you are using mobile app, in the right three dot icon to each answer. In the meantime, seeing that you shout for help about promise, here is my video tutorial about Promise: https://youtu.be/s6Nd4x_RJPw
12th Jul 2020, 4:09 AM
Gordon
Gordon - avatar
+ 3
function foo() { return new Promise(( resolve , reject ) => { let result = getSomeResult(); if (result) resolve('Success'); else reject('Something went wrong'); }); }
25th Jul 2020, 4:19 PM
Ekundayo Timileyin
+ 1
Satnam Singh What exactly do you not understand in my question?
22nd Dec 2018, 9:35 AM
Solo
Solo - avatar
+ 1
i need help with this too.
11th Jul 2020, 7:46 PM
Bootstrap 4
Bootstrap 4 - avatar
0
Then is the resolve defined in promise, and variable s is the one passed from the promise catch is the reject foo().then(s=>{ console.log(s) //Success }).catch(s=>{ console.log(s) //Something went wrong })
17th Dec 2020, 8:31 PM
tieflabs
tieflabs - avatar
0
Fill in the blanks to define a function that returns a Promise object. function foo() { return new Promise(( resolve , reject ) => { let result = getSomeResult(); if (result) resolve('Success'); else reject('Something went wrong'); }); }
10th Mar 2023, 6:44 PM
Krishn Kant Sharma
Krishn Kant Sharma - avatar
- 1
sorry
11th Jul 2020, 7:48 PM
Bootstrap 4
Bootstrap 4 - avatar
- 1
function foo() { return new Promise(( resolve , reject ) => { let result = getSomeResult(); if (result) resolve('Success'); else reject('Something went wrong'); }); }
3rd Dec 2020, 7:52 PM
El Maslohi Hassan
- 1
Here is the solution: function foo() { return new Promise(( resolve , reject ) => { let result = getSomeResult(); if (result) resolve('Success'); else reject('Something went wrong'); }); }
10th Aug 2021, 10:48 AM
Owethu Sotomela
Owethu Sotomela - avatar
- 1
function foo() { return new Promise(( resolve , reject ) => { let result = getSomeResult(); if (result) resolve('Success'); else reject('Something went wrong'); }); }
7th Dec 2021, 12:01 PM
Md. Rakibul Islam
Md. Rakibul Islam - avatar