Hướng dẫn javascript change global variable in promise - javascript thay đổi biến toàn cầu trong lời hứa

Nếu bạn muốn truy cập phiên bản đã thay đổi của myval (nghĩa là thực hiện mã của bạn sau khi bài tập bên trong lời hứa đã được thực hiện), bạn cần phải theo dõi một then khác hoặc làm bất cứ điều gì khác sẽ đặt mã của bạn vào Hàng đợi sự kiện sau khi chuyển nhượng.

var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
});

setTimeout(() => console.log(myval), 0); // logs "foo"
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});

Và một ví dụ đang chờ đợi, có lẽ là cái bạn đang tìm kiếm:

  • Bọc mọi thứ vào một chức năng Async ngay lập tức, vì vậy chúng ta có thể sử dụng await bên trong
  • Lưu lời hứa .then vào một biến (rõ ràng bạn có thể bỏ qua trực tiếp biến đó và await)
  • await Lời hứa đó trước
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="a";
    promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    }).then(function() {
        console.log(myval) // logs "foo"
    });
    
    1 của bạn
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();

Đây là thứ tự mã của bạn đang chạy:

// 1
var global = [];

// 2
fetch(URL)

// 3
const fun = () => console.log(global);
    
// 4 (once the fetch promise completes)   
    .then(data => {
        global = data
    })
    .catch(err => console.error(err));

Fetch đang gọi URL không đồng bộ, có nghĩa là nó thực hiện cuộc gọi và ngay lập tức chuyển sang dòng tiếp theo (đó là

var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
1).

Khi yêu cầu tìm nạp kết thúc, cuộc gọi lại then với

var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
4 được gọi.

Bạn cần phải đặt câu lệnh

var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
1 của mình bên trong chức năng gọi lại then của mình hoặc sử dụng Async/đang chờ thử/bắt:

var global = [];

getData();

async function getData() {
    try {
        const data = await fetch(URL);
    } catch (err) {
        console.error(err)
        throw err;
    }
    global = data;
    console.log(global);
}

var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
7

var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
8
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
9
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
0

var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
8
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
2
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
0

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
8
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
6
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
0

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
8
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
9

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
8
// 1
var global = [];

// 2
fetch(URL)

// 3
const fun = () => console.log(global);
    
// 4 (once the fetch promise completes)   
    .then(data => {
        global = data
    })
    .catch(err => console.error(err));
1

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
8
// 1
var global = [];

// 2
fetch(URL)

// 3
const fun = () => console.log(global);
    
// 4 (once the fetch promise completes)   
    .then(data => {
        global = data
    })
    .catch(err => console.error(err));
3

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4
// 1
var global = [];

// 2
fetch(URL)

// 3
const fun = () => console.log(global);
    
// 4 (once the fetch promise completes)   
    .then(data => {
        global = data
    })
    .catch(err => console.error(err));
5
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
6
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
0

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
8
var global = [];

getData();

async function getData() {
    try {
        const data = await fetch(URL);
    } catch (err) {
        console.error(err)
        throw err;
    }
    global = data;
    console.log(global);
}
0
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
0

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
8
var global = [];

getData();

async function getData() {
    try {
        const data = await fetch(URL);
    } catch (err) {
        console.error(err)
        throw err;
    }
    global = data;
    console.log(global);
}
3

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
8
var global = [];

getData();

async function getData() {
    try {
        const data = await fetch(URL);
    } catch (err) {
        console.error(err)
        throw err;
    }
    global = data;
    console.log(global);
}
5

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
8
var global = [];

getData();

async function getData() {
    try {
        const data = await fetch(URL);
    } catch (err) {
        console.error(err)
        throw err;
    }
    global = data;
    console.log(global);
}
7

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
8
var global = [];

getData();

async function getData() {
    try {
        const data = await fetch(URL);
    } catch (err) {
        console.error(err)
        throw err;
    }
    global = data;
    console.log(global);
}
9

myval0myval1

myval0myval3

myval0myval5

myval0myval7

myval0myval9

myval0then1

myval0then3

myval0then5

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4then7

myval0

var global = [];

getData();

async function getData() {
    try {
        const data = await fetch(URL);
    } catch (err) {
        console.error(err)
        throw err;
    }
    global = data;
    console.log(global);
}
3

myval0await1

myval0await3

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
8await5

await6await7

myval0await9

.then0.then1

.then0.then3

myval0.then5

myval0.then7

myval0.then9

myval0then5

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
8then7

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4
// 1
var global = [];

// 2
fetch(URL)

// 3
const fun = () => console.log(global);
    
// 4 (once the fetch promise completes)   
    .then(data => {
        global = data
    })
    .catch(err => console.error(err));
5
var global = [];

getData();

async function getData() {
    try {
        const data = await fetch(URL);
    } catch (err) {
        console.error(err)
        throw err;
    }
    global = data;
    console.log(global);
}
0
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
0

// 1
var global = [];

// 2
fetch(URL)

// 3
const fun = () => console.log(global);
    
// 4 (once the fetch promise completes)   
    .then(data => {
        global = data
    })
    .catch(err => console.error(err));
5
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
2
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
0

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
8
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
09
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
03
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
04
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
122

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
8
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
15

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4
// 1
var global = [];

// 2
fetch(URL)

// 3
const fun = () => console.log(global);
    
// 4 (once the fetch promise completes)   
    .then(data => {
        global = data
    })
    .catch(err => console.error(err));
5
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
09
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
19

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
8
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
22
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
23
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
22
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
19

Các

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
8
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
38
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
39
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
38
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
19

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
8
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
22
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
45
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
22
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
19

Các

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
8
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
38
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
39
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
38
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
19

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
8
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
66
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
67
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
04
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
69
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
70
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
66
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
19

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
8
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
66
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
67
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
04
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
78
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
79
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
66
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
0

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
8
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
84
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
32
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
04
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
87

(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
4
// 1
var global = [];

// 2
fetch(URL)

// 3
const fun = () => console.log(global);
    
// 4 (once the fetch promise completes)   
    .then(data => {
        global = data
    })
    .catch(err => console.error(err));
5
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
84
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
0

// 1
var global = [];

// 2
fetch(URL)

// 3
const fun = () => console.log(global);
    
// 4 (once the fetch promise completes)   
    .then(data => {
        global = data
    })
    .catch(err => console.error(err));
5
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
02
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
0

// 1
var global = [];

// 2
fetch(URL)

// 3
const fun = () => console.log(global);
    
// 4 (once the fetch promise completes)   
    .then(data => {
        global = data
    })
    .catch(err => console.error(err));
5
var promise1 = new Promise(function(resolve, reject) {
    resolve('foo');
});
let myval="a";
promise1.then(function(value) {
 myval=value;
  console.log(value); // this logs "foo"
}).then(function() {
    console.log(myval) // logs "foo"
});
9
(async () => {
    var promise1 = new Promise(function(resolve, reject) {
        resolve('foo');
    });
    let myval="";
    var thenedPromise = promise1.then(function(value) {
     myval=value;
      console.log(value); // this logs "foo"
    });

    await thenedPromise; // wait before the promise generated by "then" is resolved

    console.log(myval); // logs "foo"
})();
0

Globalthis là gì?

Thuộc tính Globalthis cung cấp một cách tiêu chuẩn để truy cập giá trị toàn cầu này (và do đó chính đối tượng toàn cầu) trên các môi trường. Không giống như các thuộc tính tương tự như cửa sổ và bản thân, nó được đảm bảo hoạt động trong bối cảnh cửa sổ và không cửa sổ.provides a standard way of accessing the global this value (and hence the global object itself) across environments. Unlike similar properties such as window and self , it's guaranteed to work in window and non-window contexts.

Có phải toàn cầu trong JavaScript?

Cho phép bạn khai báo các biến được giới hạn trong phạm vi của câu lệnh khối hoặc biểu thức mà nó được sử dụng, không giống như từ khóa VAR, tuyên bố một biến trên toàn cầu hoặc cục bộ với toàn bộ hàm bất kể phạm vi khối., unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.

Các vấn đề với các biến toàn cầu trong JavaScript là gì?

Điều này là do các biến toàn cầu dễ dàng được ghi đè bởi các tập lệnh khác.Các biến toàn cầu không tệ và thậm chí không phải là mối quan tâm bảo mật, nhưng nó không nên ghi đè lên các giá trị của một biến khác.Về việc sử dụng các biến toàn cầu hơn trong mã của chúng tôi, nó có thể dẫn đến vấn đề bảo trì.global variables are easily overwritten by other scripts. Global Variables are not bad and not even a security concern, but it shouldn't overwrite values of another variable. On the usage of more global variables in our code, it may lead to a maintenance issue.

Các biến toàn cầu là gì các biến được khai báo trong JavaScript là gì?

Các biến toàn cầu là các biến có thể được truy cập từ bất cứ nơi nào trong chương trình.Đây là các biến được khai báo trong phần thân chính của mã nguồn và bên ngoài tất cả các chức năng.Các biến này có sẵn cho mọi chức năng để truy cập.Từ khóa VAR được sử dụng để khai báo các biến trên toàn cầu.the variables that can be accessed from anywhere in the program. These are the variables that are declared in the main body of the source code and outside all the functions. These variables are available to every function to access. Var keyword is used to declare variables globally.