0
What is wrong in this code?
15 odpowiedzi
+ 2
hi Sina
first you need to fix typo ,
promise ==> should be Promise
then you need to use resolve and reject
Regards
+ 2
navigator.getBattery().then(function(battery) { document.write(battery.level * 100); } )
see this is a promise too but it dowsn't has result or resolve
+ 2
Warn here you are USING the promise and in this case it accept only the resolved callback (the function passed to .then)
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getBattery
+ 1
That its empty :)
+ 1
We need your code to take a look and help you🙄
+ 1
☺☺☺excuse me I forgot
+ 1
You have forget to use resolve (and if needed reject) and use right syntax for Promise (not promise):
function f0(a){
return new Promise((resolve , reject)=>{
var x = 0;
if(x % 2 == 0){
x = a*5
}
else{
x = a*10
}
resolve(x)
})
}
f0(5).then((result)=>{
console.log(result)
})
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
+ 1
Sina Have you added resolve(x) at end like in my sample code?
+ 1
Sina dont worry.. He has contacted me for notify something... Dont take cure, we have removed comments
+ 1
if I want to write it with resolve and result how it is going to be?
0
why it doesn't console?
0
yes you say true
0
Also we can write this code in another code.
so why do we must learn promis in js?
0
what?