+ 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()); } }); }
24 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
})
+ 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?
+ 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.
+ 6
+ 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
+ 4
// Zohir
You have not been taught that speaking out about a person in the third person in his presence is indecent?
+ 4
Taste
Thank you for the clarification.
Could show an example how to create a new promise every time we call a function.
+ 4
Gordon ☺
Thank you!
I easily disassembled your example, I meant the code "function foo()"
+ 3
Satnam Singh Maybe he want than we correct this code ?
+ 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.
+ 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
+ 3
function foo() {
return new Promise((
resolve , reject
) => {
let result = getSomeResult();
if (result)
resolve('Success');
else
reject('Something went wrong');
});
}
+ 1
Satnam Singh
What exactly do you not understand in my question?
+ 1
i need help with this too.
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
})
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');
});
}
- 1
sorry
- 1
function foo() {
return new Promise((
resolve
,
reject
) => {
let result = getSomeResult();
if (result)
resolve('Success');
else
reject('Something went wrong');
});
}
- 1
Here is the solution:
function foo() {
return new Promise((
resolve
,
reject
) => {
let result = getSomeResult();
if (result)
resolve('Success');
else
reject('Something went wrong');
});
}
- 1
function foo() {
return new Promise((
resolve
,
reject
) => {
let result = getSomeResult();
if (result)
resolve('Success');
else
reject('Something went wrong');
});
}