-
Notifications
You must be signed in to change notification settings - Fork 0
/
16_asyncAwait.js
41 lines (37 loc) · 1.14 KB
/
16_asyncAwait.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
let potos = [];
// function fotoUp(){
// let uploadStat = new Promise((resolve, reject)=> {
// setTimeout(()=>{
// potos.push(' gambar xx');
// resolve(' status: foto terupload ✌️')
// },4000)
// })
// let result = uploadStat
// console.log(result);
// console.log(potos.length);
// console.log(potos)
// }
// output : result = Pending, length =0; potos = kosong
// karna func fotoUp tetap menjalankan kode ke bawah walopun ada fungsi uploadStat
// yang masih berjalan 4000ms.
// makanya fungsi didalam uploadStat tidak terjalankan.karna JS melewati dan
// menjalankan fungsi2 dibawahnya
async function fotoUp(){
try{
let uploadStat = new Promise((resolve, reject)=> {
setTimeout(()=>{
potos.push(' gambar xx');
resolve(' status: foto terupload ✌️')
},4000)
});
let result = await uploadStat
console.log(result);
console.log(potos.length);
console.log(potos);
}
catch (error){
console.log(`error: ${error}`)
}
}
fotoUp();
// make promise a sync await