Một cái gì đó tôi đã đấu tranh trong quá khứ và đang vật lộn với ngày hôm nay là ngăn chặn API/AJAX tiếp tục cho đến khi bạn nhận được phản hồi của mình. Hiện tại tôi đang làm việc với API Facebook. Tôi cần nhận được phản hồi từ một cuộc gọi sau đó trả lại nhưng điều xảy ra là chức năng của tôi đang quay trở lại trước khi tôi nhận được phản hồi từ cuộc gọi API. Tôi biết tại sao nó lại xảy ra, tôi không thể tìm ra cách ngăn chặn nó! Đây là mã của tôi ...
function makeCall[]{
var finalresponse = "";
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
}];
return finalresponse;
}
// ----- CHỈNH SỬA
Tôi nhận thấy một số người đề nghị một cái gì đó như thế này ...
function makeCall[]{
var finalresponse = "";
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
return finalresponse;
} else {
finalresponse = 'Post ID: ' + response.id;
return finalresponse;
}
}];
}
Nhưng điều này trả về không xác định
// Chỉnh sửa dựa trên các bản cập nhật
function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
Điều kiện tiên quyết: Hàm Async/Await trong JavaScriptAsync/Await Function in Javascript
JavaScript là một ngôn ngữ đồng bộ, tức là JavaScript thực thi một dòng mã tại một thời điểm. Nó xếp hàng các sự kiện trong hành động và thực hiện chúng theo thứ tự. Nhưng trong một số trường hợp, JavaScript hoạt động không đồng bộ, chẳng hạn như trong các cuộc gọi Ajax hoặc Axios. Nó thực hiện một cuộc gọi đến API nhưng không chờ đợi API trả về kết quả và tiến triển với sự kiện được xếp hàng tiếp theo.synchronous language, i.e. JavaScript executes one line of code at a time. It queues the events in action and perform them in order. But in some case, Javascript behaves as asynchronous, such as in AJAX or Axios calls. It makes a call to the API but does not wait for the API to return result, and progresses with the next queued event.
Chức năng ASYNC/AIDIT trong JavaScript sẽ cung cấp hỗ trợ trì hoãn chương trình cho các cuộc gọi API đó. Hàm ASYNC và chờ đợi thực hiện theo nguyên tắc hứa hẹn trong JavaScript. will provide assistance delaying the program for such API calls. Async and Await function perform on the principle of promises in JavaScript.
- ASYNC: Nó làm cho JavaScript thực thi các mã dựa trên lời hứa như thể chúng đồng bộ và trả về một giá trị mà không phá vỡ luồng thực thi. Nếu không có giá trị nào được trả lại, nó sẽ kết thúc lời hứa và tiếp tục thực hiện bình thường. It makes javascript execute promise based codes as if they were synchronous and return a value without breaking the execution thread. If no value is returned, it wraps the promise and resumes the normal execution.
- Đang chờ: Nó được sử dụng để chờ đợi một lời hứa trở lại. Nó chỉ được sử dụng bên trong khối async. It is used to wait for a promise to return. It is used only inside the async block.
Kịch bản Python: Chúng tôi sẽ thực hiện một cuộc gọi đến API Python được lưu trữ trên localhost trả về một câu cơ bản. Mã cho API như sau: We will be making a call to a Python API hosted on the localhost which returns a basic sentence. The code for the API is as follows:
from
flask
import
Flask, jsonify, request
from
flask_cors
import
function makeCall[]{
var finalresponse = "";
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
return finalresponse;
} else {
finalresponse = 'Post ID: ' + response.id;
return finalresponse;
}
}];
}
0function makeCall[]{
var finalresponse = "";
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
return finalresponse;
} else {
finalresponse = 'Post ID: ' + response.id;
return finalresponse;
}
}];
}
1function makeCall[]{
var finalresponse = "";
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
return finalresponse;
} else {
finalresponse = 'Post ID: ' + response.id;
return finalresponse;
}
}];
}
2 function makeCall[]{
var finalresponse = "";
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
return finalresponse;
} else {
finalresponse = 'Post ID: ' + response.id;
return finalresponse;
}
}];
}
3function makeCall[]{
var finalresponse = "";
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
return finalresponse;
} else {
finalresponse = 'Post ID: ' + response.id;
return finalresponse;
}
}];
}
4function makeCall[]{
var finalresponse = "";
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
return finalresponse;
} else {
finalresponse = 'Post ID: ' + response.id;
return finalresponse;
}
}];
}
5function makeCall[]{
var finalresponse = "";
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
return finalresponse;
} else {
finalresponse = 'Post ID: ' + response.id;
return finalresponse;
}
}];
}
6function makeCall[]{
var finalresponse = "";
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
return finalresponse;
} else {
finalresponse = 'Post ID: ' + response.id;
return finalresponse;
}
}];
}
7function makeCall[]{
var finalresponse = "";
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
return finalresponse;
} else {
finalresponse = 'Post ID: ' + response.id;
return finalresponse;
}
}];
}
8function makeCall[]{
var finalresponse = "";
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
return finalresponse;
} else {
finalresponse = 'Post ID: ' + response.id;
return finalresponse;
}
}];
}
2function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
0function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
1function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
2function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
3 function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
4function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
5function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
6 function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
7function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
8function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
9from
0from
1
from
2 from
3
function makeCall[]{
var finalresponse = "";
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
return finalresponse;
} else {
finalresponse = 'Post ID: ' + response.id;
return finalresponse;
}
}];
}
2function makeCall[]{
var finalresponse = "";
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
return finalresponse;
} else {
finalresponse = 'Post ID: ' + response.id;
return finalresponse;
}
}];
}
2 from
6from
7function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
5from
9function makeCall[]{
var finalresponse = "";
FB.api['/me/feed', 'post', { message: body }, function[response] {
if [!response || response.error] {
finalresponse = response.error;
return finalresponse;
} else {
finalresponse = 'Post ID: ' + response.id;
return finalresponse;
}
}];
}
2 flask
1flask
2Lưu ý: Bạn phải cài đặt các gói sau để có thể lưu trữ API này: Flask, Jsonify, Request, Flask_cors. Chỉ cần chạy mã Python để bắt đầu API trên //127.0.0.1:5000/ You must have the following packages installed to be able to host this API: flask, jsonify, request, flask_cors. Simply run the python code to start the API on //127.0.0.1:5000/
SCRIPT JS: Mã dưới đây là chương trình JavaScript để gọi API mà không có chức năng ASYNC/AIDIT. The below code is a Javascript program to call the API without Async/Await function.
flask
3 flask
4
function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
5flask
6flask
7flask
8
flask
9import
0 import
1
flask
9import
3import
4import
5
flask
9
function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
6 import
8flask
7Flask, jsonify, request
0
flask
7Flask, jsonify, request
2
flask
9Flask, jsonify, request
4
flask
7Flask, jsonify, request
6
function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
5import
5Flask, jsonify, request
6
flask
3 from
1
function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
5from
3function share_share[action_id]{
var finalresponse = makeCall[action_id, process];
return finalresponse;
}
function makeCall[action_id, callback]{
var body = 'Test post';
FB.api['/me/feed', 'post', { message: body }, function [response] {
if [!response || response.error] {
var finalresponse = response.error;
} else {
finalresponse = 'Post ID: ' + response.id;
}
callback[action_id, finalresponse];
}];
}
function process[action_id, finalresponse]{
console.log[finalresponse];
}
5import
3from
6import
5Flask, jsonify, request
6
from
9
Đầu ra dự kiến:
Đầu ra thu được:
Giải thích: Điều này xảy ra bởi vì JavaScript kêu gọi API, đã biến nó thành một quy trình riêng biệt và sau đó tiếp tục. Nó tìm thấy console.log [phản hồi] ;, nhưng phản hồi đã không được ký vào thời điểm đó, vì vậy bảng điều khiển hiển thị không xác định. Chương trình đã tiến triển và in ‘Tuyên bố 2. Vào thời điểm này, API thậm chí đã không đạt được tuyên bố xử lý. Đây là nơi JavaScript hành xử như một ngôn ngữ không đồng bộ. Vấn đề này được khắc phục bằng cách sử dụng các chức năng ASYNC/đang chờ. Mã tương ứng để thiết lập một lời hứa được đưa ra dưới đây. This happens because JavaScript called for the API, made it a separate process and then proceeded further. It found console.log[response];, but the response was unsigned by the time, so the console shows undefined. The program progressed and printed ‘Statement 2’. By this time, the API had not even reached the processing statement. This is where Javascript behaves as an asynchronous language. This problem is rectified using Async/Await functions. The corresponding code to establish a promise is given below.
- Program:
flask
3flask
4flask
9import
0import
1flask
7flask
6flask
9
6function share_share[action_id]{ var finalresponse = makeCall[action_id, process]; return finalresponse; } function makeCall[action_id, callback]{ var body = 'Test post'; FB.api['/me/feed', 'post', { message: body }, function [response] { if [!response || response.error] { var finalresponse = response.error; } else { finalresponse = 'Post ID: ' + response.id; } callback[action_id, finalresponse]; }]; } function process[action_id, finalresponse]{ console.log[finalresponse]; }
import
8flask
3from
1import
2import
3import
4import
5import
2
00function makeCall[]{ var finalresponse = ""; FB.api['/me/feed', 'post', { message: body }, function[response] { if [!response || response.error] { finalresponse = response.error; return finalresponse; } else { finalresponse = 'Post ID: ' + response.id; return finalresponse; } }]; }
flask
9Flask, jsonify, request
0import
2Flask, jsonify, request
2import
2
06function makeCall[]{ var finalresponse = ""; FB.api['/me/feed', 'post', { message: body }, function[response] { if [!response || response.error] { finalresponse = response.error; return finalresponse; } else { finalresponse = 'Post ID: ' + response.id; return finalresponse; } }]; }
flask
9Flask, jsonify, request
6flask
7import
5
5function share_share[action_id]{ var finalresponse = makeCall[action_id, process]; return finalresponse; } function makeCall[action_id, callback]{ var body = 'Test post'; FB.api['/me/feed', 'post', { message: body }, function [response] { if [!response || response.error] { var finalresponse = response.error; } else { finalresponse = 'Post ID: ' + response.id; } callback[action_id, finalresponse]; }]; } function process[action_id, finalresponse]{ console.log[finalresponse]; }
12function makeCall[]{ var finalresponse = ""; FB.api['/me/feed', 'post', { message: body }, function[response] { if [!response || response.error] { finalresponse = response.error; return finalresponse; } else { finalresponse = 'Post ID: ' + response.id; return finalresponse; } }]; }
Flask, jsonify, request
6Đầu ra dự kiến:
5function share_share[action_id]{ var finalresponse = makeCall[action_id, process]; return finalresponse; } function makeCall[action_id, callback]{ var body = 'Test post'; FB.api['/me/feed', 'post', { message: body }, function [response] { if [!response || response.error] { var finalresponse = response.error; } else { finalresponse = 'Post ID: ' + response.id; } callback[action_id, finalresponse]; }]; } function process[action_id, finalresponse]{ console.log[finalresponse]; }
18function makeCall[]{ var finalresponse = ""; FB.api['/me/feed', 'post', { message: body }, function[response] { if [!response || response.error] { finalresponse = response.error; return finalresponse; } else { finalresponse = 'Post ID: ' + response.id; return finalresponse; } }]; }
5function share_share[action_id]{ var finalresponse = makeCall[action_id, process]; return finalresponse; } function makeCall[action_id, callback]{ var body = 'Test post'; FB.api['/me/feed', 'post', { message: body }, function [response] { if [!response || response.error] { var finalresponse = response.error; } else { finalresponse = 'Post ID: ' + response.id; } callback[action_id, finalresponse]; }]; } function process[action_id, finalresponse]{ console.log[finalresponse]; }
import
3from
6import
5Flask, jsonify, request
6from
9 - Output:
Đầu ra thu được: The async function made the function to continue execution on the same thread without breaking into a separate event. The await keyword marks the point where the program has to wait for the promise to return. Hence, the program waits for the API to return response and then proceeded with the program, therefore, the output is in perfect order as required.