Javascript:代码不等待异步等待

时间:2018-12-24 08:02:55

标签: javascript async-await

我正在创建一个Web应用程序,其中在一个场景中使用async await

外观如何

async function fnDrawHotspotFromCords(cords, qno, insertedHeightWidth, ansCordinates, shapeType, currentResult) {
    if ($('#image-canvas-' + qno).children().length > 0)
        return;
    await setImageHeightWidth(qno, insertedHeightWidth); //await for this function
    if (cords != '') {
        var arrCords = cords.split('[.]');
        for (var i = 0; i < arrCords.length; i++) {
            var arritem = arrCords[i].split('[,]');
            var markerPositionTop = Number(arritem[1]);
            var markerPositionLeft = Number(arritem[0]);

            var perW = insertedHeightWidth.split(',')[1]
                / Number(window.getComputedStyle(document.getElementById('image-canvas-' + qno)).getPropertyValue('width').replace('px', ''));

            var perH = insertedHeightWidth.split(',')[0]
                / Number(window.getComputedStyle(document.getElementById('image-canvas-' + qno)).getPropertyValue('height').replace('px', ''));
            if (!isNaN(perH)) {
                markerPositionTop = parseFloat(markerPositionTop / perH).toFixed(0);
                markerPositionLeft = parseFloat(markerPositionLeft / perW).toFixed(0);
            }

            var updatedTopPosition = Number(markerPositionTop) - 12;
            var updtaedLeftPosition = Number(markerPositionLeft) - 12;

            var answerStatus = 'Correct Area';
            if (currentResult == 0)
                answerStatus = '';
            var $marker = $("<span class='marker' id='hotSpotMarker' style='top: " + updatedTopPosition + "px; left: " + updtaedLeftPosition + "px; white-space: nowrap; height:26px; width:26px;'><span class='marker' style='height:18px; width:17px; left: 3px; top: 2px;'><span class='marker' style='height:9px; width:9px; left: 2px; top: 2px;'></span></span>" + answerStatus + "</span>");
            $("#image-canvas-" + qno).append($marker);
        }
    }
}
  

此函数调用以下函数

async function setImageHeightWidth(qno, insertedHeightWidth) {
    setTimeout(() => {
        if (Number(insertedHeightWidth.split(',')[0]) > $(window).height()) {
            let heightToAssign = $(window).height() * 90 / 100;
            document.getElementById('image-canvas-' + qno).style.height = heightToAssign + 'px';
        }
        else {
            document.getElementById('image-canvas-' + qno).style.height = Number(insertedHeightWidth.split(',')[0]) + 'px';
        }

        if (Number(insertedHeightWidth.split(',')[1]) > $(window).width()) {
            let widthToAssign = $(window).width() * 90 / 100;
            document.getElementById('image-canvas-' + qno).style.width = widthToAssign + 'px';
        }
        else {
            document.getElementById('image-canvas-' + qno).style.width = Number(insertedHeightWidth.split(',')[1]) + 'px';
        }
    }
        , 1000);
}
  

,但是第一个函数不等待第二个函数的执行   功能

我没有从第二个功能返回的承诺

我只想执行第二个函数,然后在执行第二个函数之后,第一个函数应继续执行。

但是我的第一个函数没有等待第二个函数完成执行

0 个答案:

没有答案
相关问题