JavaScript:如何使用异步方法?

时间:2017-06-22 19:58:41

标签: javascript node.js asynchronous async-await

我想得到结果:

1
2
3

怎么做?

我已经尝试了几种实现选项,但它们都没有产生我想要的结果。

如果使用:

var aaa = {
    line: function () {
        console.log(1)
        aaa.RF();
        console.log(3)
    },
    RF: async function () {
        await jsonfile.readFile(file, function(err, obj) {
            console.log(2)
        })
    }
}
aaa.line()

结果:

1
3
2

如果

console.log(4)
async jsonfile.readFile(file, function(err, obj) {
    await console.log(5)
})
console.log(6)

结果错误

如果

console.log(1)
async function a() {
    await jsonfile.readFile(file, function(err, obj) {
         console.log(2)
    })
}
a()
console.log(3)

结果:

1
3
2

1 个答案:

答案 0 :(得分:3)

您需要await。在不添加async的情况下,它仍会将var aaa = { line: async function () { console.log(1) await aaa.RF(); console.log(3) }, RF: async function () { await jsonfile.readFile(file, function(err, obj) { console.log(2) }) } } aaa.line() 函数视为异步并继续。

                            // JavaScript source code
            $scope.gridOptions = {
                expandableRowTemplate: 'expandableRowTemplate.html',
                expandableRowHeight: 150,
                //subGridVariable will be available in subGrid scope
                expandableRowScope: {
                    subGridVariable: 'subGridScopeVariable'
                }
            }

            $scope.gridOptions.columnDefs = [
            { name: 'check_number', field: 'check_number', displayName: 'check_number' }

            ];

            var callData = function () {
                alert('call data');
                return $http({
                    method: "post",
                    url: "/Common/Metasystem/AP/Reports/ReportData.aspx/GetcheckReportData",
                    dataType: 'json',
                    data: { beforedt: '01/01/2018', afterdt: '01/01/1900', checknumberlist: '' },
                    headers: { "Content-Type": "application/json" }
                }
                )
            };

            $scope.fetchResult =
            function () {
                // alert('call');
                $scope.gridOptions.data = 'aa';
                callData()
                .success(function (resultvalue) {
                    // $scope.resultdata = "[" + JSON.stringify(resultvalue.d) + "]";//issue
                    $scope.resultdata = JSON.parse(resultvalue.d);
                    for (i = 0; i < $scope.resultdata.length; i++) {

                        $scope.resultdata[i].subGridOptions = {
                            columnDefs: [{ name: "check_number", field: "check_number", displayName: 'check_number' }, { name: "voucher_number", field: "voucher_number", displayName: 'voucher_number' }],
                            data: $scope.resultdata[i].friends
                        }
                    }
                    $scope.gridOptions.data = $scope.resultdata;
                })
                .error(function (data) {
                    console.error('Repos error', data);
                })
            };
            $scope.fetchResult();