2021년 10월 5일 화요일

2개의 promise 비동기 순차 처리

하나의 promise가 완전히 종료된 후 나머지 promise를 처리한다.

/**
 * 2개의 promise 비동기 처리
 */
var sampleTerrainToclampToHeight = function() {
    console.time("sampleTerrainToclampToHeight time");
    console.timeLog("sampleTerrainToclampToHeight time");
    var cartesians = [cartesian1, cartesian2];

    //sampleTerrain 처리
    var promise = calculator.sampleTerrain(cartesians);
    Cesium.when(promise)
    //then1
    .then(function(updatedPositions) {
        var updatedCartesians = Cesium.Ellipsoid.WGS84.cartographicArrayToCartesianArray(updatedPositions);
        return calculator.clampToHeight(updatedCartesians); //promise를 리턴
    })
    .otherwise(function(error){
        throw new Error('otherwise1 sampleTerrain 거부: ' + error);
    })
    //then2 : 앞의 then에서 리턴한 promise 처리
    .then(function(updatedCartesians) {
        updateProcess(updatedCartesians);
        console.timeEnd("sampleTerrainToclampToHeight time");
    })
    .otherwise(function(error){
        throw new Error('otherwise2 clampToHeight 거부: ' + error);
    });
}

/*
 * 마무리 처리
 */
function updateProcess(updatedCartesians) {
    console.timeLog("sampleTerrainToclampToHeight time");
    console.time(str + 'time');
    console.timeLog(str + 'time');

    //로직 들어갈 곳

    console.timeEnd(str + 'time');
    console.timeLog("sampleTerrainToclampToHeight time");
}