Hướng dẫn wait for api call to finish javascript - đợi cuộc gọi api kết thúc javascript

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:

  • Program:

    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;
            }
          }];
    }
    
    0

    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;
            }
          }];
    }
    
    1
    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;
            }
          }];
    }
    
    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;
            }
          }];
    }
    
    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;
            }
          }];
    }
    
    4

    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;
            }
          }];
    }
    
    5
    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;
            }
          }];
    }
    
    6
    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;
            }
          }];
    }
    
    7
    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;
            }
          }];
    }
    
    8
    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;
            }
          }];
    }
    
    2
    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];
    }
    
    0
    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];
    }
    
    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];
    }
    
    2

    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];
    }
    
    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];
    }
    
    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];
    }
    
    5
    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
    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];
    }
    
    7
    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];
    }
    
    8
    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];
    }
    
    9from0from1

    from2 from3

    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;
            }
          }];
    }
    
    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;
            }
          }];
    }
    
    2 from6from7

    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];
    }
    
    5from9
    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;
            }
          }];
    }
    
    2 flask 1flask 2

  • Lư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.

  • Program:

    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 6

    flask 7flask 8

    flask 9import0 import1

    flask 9import3import4import5

    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 import8

    flask 7Flask, jsonify, request0

    flask 7Flask, jsonify, request2

    flask 9Flask, jsonify, request4

    flask 7Flask, jsonify, request6

    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];
    }
    
    5import5

    Flask, jsonify, request6

    flask 3 from1

    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];
    }
    
    5from3

    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];
    }
    
    5import3from6import5

    Flask, jsonify, request6

    from9

  • Đầ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 3 flask 4

      flask 9import0 import1

      flask 7flask 6

      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 import8

      flask 3 from1

      import2import3import4import5

      import2

      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;
              }
            }];
      }
      
      00

      flask 9Flask, jsonify, request0

      import2Flask, jsonify, request2

      import2

      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;
              }
            }];
      }
      
      06

      flask 9Flask, jsonify, request6

      flask 7import5

      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];
      }
      
      5
      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;
              }
            }];
      }
      
      12

      Flask, jsonify, request6

      Đầu ra dự kiến:

      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];
      }
      
      5
      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;
              }
            }];
      }
      
      18

      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];
      }
      
      5import3from6import5

      Flask, jsonify, request6

      from9

    • 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.


    Làm thế nào để bạn chờ đợi Async hoàn thành JavaScript?

    Bên trong hàm Async, bạn có thể sử dụng từ khóa đang chờ đợi trước khi gọi đến một hàm trả về lời hứa. Điều này làm cho mã chờ vào thời điểm đó cho đến khi lời hứa được giải quyết, tại thời điểm đó, giá trị hoàn thành của lời hứa được coi là giá trị trả lại hoặc giá trị bị từ chối được ném.use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.

    Làm thế nào để bạn chờ đợi một chức năng hoàn thành?

    Một cách thanh lịch để chờ đợi một hàm hoàn thành trước tiên là sử dụng lời hứa với chức năng ASYNC/AIDIT ...
    Thứ nhất, tạo ra một lời hứa.....
    Đối với chức năng thứ hai, bạn có thể sử dụng Async/đang chờ một hàm trong đó bạn sẽ chờ đợi chức năng đầu tiên hoàn thành trước khi tiến hành các hướng dẫn ..

    Làm thế nào để bạn tạo một chức năng chờ đợi trong JavaScript?

    Hàm setTimeout [] thực hiện độ trễ trước khi thực hiện hàm javaScript cụ thể.Nó yêu cầu hai tham số: một chức năng sau trễ phải được chạy và thời gian trễ.Hàm sau trễ phải được xác định trong tệp javascript trước khi được gọi bởi setTimeout [].. It requires two parameters: a post-delay function to be run and the delay time. The post-delay function must be defined in the Javascript file before being called by setTimeout[] .

    Làm thế nào chờ đợi cuộc gọi API hoàn thành trong React?

    Sử dụng Async đang chờ đợi trong React để đợi cho đến khi cuộc gọi API đã quyết tâm để kết xuất câu trả lời mã..
    hàm async fetchfunction [] {.
    const phản hồi = Await fetch [` // url.com`] ;.
    const json = chờ phản hồi.json [] ;.
    bắt [err] {.
    Ném err ;.
    Bảng điều khiển.log [err] ;.

    Bài Viết Liên Quan

    Chủ Đề