简体   繁体   English

任何避免javascript .then()方法的方法

[英]Any way to avoid javascript .then() method

I am new in javascript, following is my code( not specific) to explain my question 我是javascript新手,以下是我的代码(非特定)来解释我的问题

cornerstone.loadImage(imageIdpro).then(function(image) {//doSomething}

I already know that .then(function()) is asynchronous function, my question is, is there any way that I can modify this code to avoid to use .then() , and change this to synchronous? 我已经知道.then(function())是异步函数,我的问题是,有什么办法可以修改此代码以避免使用.then()并将其更改为同步吗? Any help appreciated. 任何帮助表示赞赏。

The reason I do not want to use .then() is because I want to slide to change some value to keep update the result when I drag slide 我不想使用.then()的原因是因为我想滑动更改一些值以在拖动幻灯片时保持更新结果

$("#upperBound").slider({
  range: "max",
  min: minPixelValue,
  max: maxPixelValue,
  slide: function(event, ui) {
    $("#rangeUpper").html(ui.value);
    currentUpperBound = ui.value;//currentUpperBound is current slide value
    console.log("1");
    loadAndViewImagethresholding(imageId);
    console.log("4");
  },
});

and loadAndViewImagethresholding function is something like that: loadAndViewImagethresholding函数是这样的:

function loadAndViewImagethresholding(imageId) {
  var imageIdpro = "wadouri:" + "http://localhost:8888/dicomread/temp/" + loadfileName;
  cornerstone.loadImage(imageIdpro).then(function(image) {
    console.log("2");
    upper = currentUpperBound;//upperBound slide current value
    lower = currentLowerBound;//there is another lowerBound slide current value
    for (var i = 0; i < image.getPixelData().length; i++) {
      if (image.getPixelData()[i] < lower || image.getPixelData()[i] > upper) {
        image.getPixelData()[i] = image.minPixelValue;
      } else {
        image.getPixelData()[i] = image.maxPixelValue;//Here is the problem!!image.getPixelData()[i] is a image pixel value at [i], every time when slide current value (upper or lower value)changes, this image.getPixelData()[i] has already changed 
      }
    }
    //after that for loop, image.getPixelData(which is a data array) has already changed ( after each time slide value update)
    cornerstone.displayImage(elementthresh, image);//but this function seems has been skipped
    console.log("3");
  });
}

and because .then() is asynchronous function, this code will implement( number in console) like: 1-4-2-3 rather than 1-2-3-4 as I expect which seems skip displayImage(); 并且因为.then()是异步函数,所以该代码将实现(控制台中的数字),例如:1-4-2-3而不是1-2-3-4,正如我期望的那样,这似乎跳过displayImage(); (update result) So that is way I am thinking if I can change this asynchronous function to synchronous, it will implement in 'right' order, and it will keep updating when I drag the slide bar. (更新结果)所以这是我在想是否可以将此异步功能更改为同步功能,它将以“正确”的顺序实现,并且在拖动滑动条时会不断更新。

Keep chaining the promises. 继续兑现承诺。 That's how they're designed to work. 这就是他们设计工作的方式。 Note the return statement in the function to return the promise for future chaining. 请注意函数中的return语句以返回对将来链接的承诺。

$("#upperBound").slider({
    range: "max",
    min: minPixelValue,
    max: maxPixelValue,
    slide: function(event, ui) {
        $("#rangeUpper").html(ui.value);
        currentUpperBound = ui.value;
        console.log("1");
        loadAndViewImagethresholding(imageId).then(function() {
            console.log("4");
        });
    },
});

function loadAndViewImagethresholding(imageId) {
    var imageIdpro = "wadouri:" + "http://localhost:8888/dicomread/temp/" + loadfileName;
    return cornerstone.loadImage(imageIdpro).then(function(image) {
        console.log("2");
        upper = currentUpperBound;
        lower = currentLowerBound;
        for (var i = 0; i < image.getPixelData().length; i++) {
            if (image.getPixelData()[i] < lower || image.getPixelData()[i] > upper) {
                image.getPixelData()[i] = image.minPixelValue;
            } else {
                image.getPixelData()[i] = image.maxPixelValue;
            }
        }
        cornerstone.displayImage(elementthresh, image);
        console.log("3");
    });
}

In the answers below, as I am not sure why currentUpperBound, currentLowerBound, upper and lower seem to be globals in your code, I've tried to write code that "respects" that fact 在下面的答案中,由于我不确定为什么currentUpperBound,currentLowerBound,upper和lower在您的代码中似乎是全局变量,因此我尝试编写“尊重”这一事实的代码

This code will make sure that each loadImage completes before the next one is started, by chaining the promises, using var p 通过使用var p链接诺言,此代码将确保每个loadImage在下一个启动之前完成。

var p = Promise.resolve();
var loading = [];

$("#upperBound").slider({
    range: "max",
    min: minPixelValue,
    max: maxPixelValue,
    slide: function(event, ui) {
        $("#rangeUpper").html(ui.value);
        var upperBound = currentUpperBound = ui.value;
        var lowerBound = currentLowerBound;
        p = p.then(function() {
            loadAndViewImagethresholding(imageId, upperBound, lowerBound);
        }
    },
});

function loadAndViewImagethresholding(imageId, upperBound, lowerBound) {
    var imageIdpro = "wadouri:" + "http://localhost:8888/dicomread/temp/" + loadfileName;
    return cornerstone.loadImage(imageIdpro).then(function(image) {
        for (var i = 0; i < image.getPixelData().length; i++) {
            lower = lowerBound;
            upper = upperBound;
            if (image.getPixelData()[i] < lower || image.getPixelData()[i] > upper) {
                image.getPixelData()[i] = image.minPixelValue;
            } else {
                image.getPixelData()[i] = image.maxPixelValue;
            }
        }
        cornerstone.displayImage(elementthresh, image);
    });
}

However, I suspect that you want to "cancel" any loadImage in progress if the slider moves before the image is loaded. 但是,我怀疑如果滑块在加载图像之前移动,您想“取消”正在进行的任何loadImage。 While you can't cancel the loadImage function, you can prevent the subsequent .then form executing - it's a bit fiddly, but I can't think of a better way off the top of my head 虽然您不能取消loadImage函数,但是可以防止随后的.then表单执行-有点麻烦,但是我想不出更好的方法

var loading = [];

$("#upperBound").slider({
    range: "max",
    min: minPixelValue,
    max: maxPixelValue,
    slide: function(event, ui) {
        // stops any .then's yet to be fired
        loading.forEach(function(x) {
            x.ok = false;
        });
        loading = [];
        $("#rangeUpper").html(ui.value);
        currentUpperBound = ui.value;
        loadAndViewImagethresholding(imageId, currentUpperBound, currentLowerBound);
    },
});

function loadAndViewImagethresholding(imageId, upperBound, lowerBound) {
    var imageIdpro = "wadouri:" + "http://localhost:8888/dicomread/temp/" + loadfileName;
    var x = { ok: true; }
    cornerstone.loadImage(imageIdpro).then(function(image) {
        if (x.ok) {
            lower = lowerBound;
            upper = upperBound;
            for (var i = 0; i < image.getPixelData().length; i++) {
                if (image.getPixelData()[i] < lower || image.getPixelData()[i] > upper) {
                    image.getPixelData()[i] = image.minPixelValue;
                } else {
                    image.getPixelData()[i] = image.maxPixelValue;
                }
            }
            cornerstone.displayImage(elementthresh, image);
        }
    });
    loading.push(x);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法避免使用JSDoc“@method”注释 - Is There Any Way to Avoid Using the JSDoc “@method” Annotation 有什么方法可以避免for循环? (ES6 / JavaScript) - Any way to avoid a for loop? (ES6 / JavaScript) 有什么方法可以避免Javascript对象中的所有“this”? - Any way to avoid all the “this” in Javascript objects? 两个 JavaScript 函数“等待”解决某些问题,有什么办法可以避免吗? - Two JavaScript funtions "await" for something that will resolve, is there any way to avoid it? 有没有办法恢复 Vanilla JavaScript 中的 preventDefault 方法? - Is there any way to revert preventDefault method in Vanilla JavaScript? 避免 JavaScript 中的重复 - Avoid duplication in JavaScript way 有什么方法可以从对象的方法中删除或使JavaScript对象无效? - Is there any way to delete or nullify a JavaScript object from the object's method? 有什么方法可以像常规 function 一样调用 JavaScript class 的方法吗? - Is there any way I can invoke the method of a JavaScript class as a regular function? 在javascript中是否有任何方法/方法可以动态地将子节点添加到列表元素中? - Is there any method/way in javascript to add a child node to list element dynamically? 有没有办法像JQuery中的链接方法那样有效地表达Javascript代码? - Is there any way to efficiently express the Javascript code like the chaining method in JQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM