简体   繁体   中英

other parts of the code execute before $http.get completed

I want to send a get method to find the server date and then get that value for a variable and use them in my latter part of the program but everytime i use the following way it executes latter part of the code before. how to stop this?

var syncedDate;
    $http.get("../../system/info").then(function (response) {
        response = response.data;
         syncedDate = response.serverDate.split("T")[0] + " (" + response.serverDate.split("T")[1].split(".")[0] + ")";
        console.log(syncedDate);
    });
    if (respo == "error") {
        syncHistory.push({
            syncDate: syncedDate,
            notification: "Error In Network",
            instance: instance,
            metaDataFilterd: filData,
            response: respo
        })

this executes asynchronously and non-blocking inherently. you may want to move your 'latter' code within the callback function (response). I'm assuming you are using angularjs, you may also want to use the errorCallback instead see

https://docs.angularjs.org/api/ng/service/ $http

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM