简体   繁体   English

在Javascript Promise内部自动调用的函数

[英]Function Automatically Called Inside Javascript Promise

I'm fairly new to Javascript promises, and running into an issue that I can't hunt down anything about via Google or Stack Exchange. 我对Javascript承诺还很陌生,遇到了一个问题,我无法通过Google或Stack Exchange寻找任何东西。 When I refer to a function in a .then chained off a promise, I sometimes have to wrap that function in an anonymous function in order to stop it from automatically executing before the original promise is resolved. 当我在.then中引用一个函数时,有时会不得不将其包装在一个匿名函数中,以阻止该函数在原始的Promise解析之前自动执行。 For example, if my code is 例如,如果我的代码是

function deleteAdvertiser(advertiser) {
    dialogService.Confirm(null, 'Delete')
        .then(advertiserService.deleteAdvertiser(advertiser))
        .then(getAdvertisers);
}

the call to advertiserService.deleteAdvertiser will automatically fire before I resolve the promise from the dialogService.Confirm. 在我解决来自dialogService.Confirm的承诺之前,对AdvertiserService.deleteAdvertiser的调用将自动触发。 However, if I write it as 但是,如果我写成

function deleteAdvertiser(advertiser) {
    dialogService.Confirm(null, 'Delete')
        .then(function () {
            advertiserService.deleteAdvertiser(advertiser)
                .then(getAdvertisers);
            });
}

it behaves as expected: the advertiserService.deleteAdvertiser call doesn't happen until I resolve the dialogService.Confirm promise (in this case by clicking the "Delete" button inside my confirmation dialog). 它的行为符合预期:在我解决dialogService.Confirm promise(在这种情况下,通过单击我的确认对话框中的“删除”按钮)之前,不会进行AdvertiserService.deleteAdvertiser调用。

The dialogService uses ngDialog's .openConfirm method, which returns a promise. dialogService使用ngDialog的.openConfirm方法,该方法返回一个promise。 I've verified that this promise is returning correctly. 我已验证此诺言正确返回。 My best guess as to why this is happening is that my need to pass along an advertiser object from the UI (the function is initially called via an ng-click on a trash can button) means that I have to call advertiserService.deleteAdvertiser(advertiser)-- that is to say, with an argument passed in-- which in turn means that JS is executing that function call as soon as it reads it, rather than just storing the reference to use later when the initial promise is resolved. 关于发生这种情况的最佳猜测是,我需要从UI传递广告客户对象(该功能最初是通过ng-click垃圾桶按钮进行调用),这意味着我必须调用AdvertiserService.deleteAdvertiser(advertiser )-也就是说,传入了一个参数-这反过来意味着JS会在读取函数后立即执行该函数调用,而不是仅存储要在初始诺言解决后使用的引用。

Am I correct in my understanding of why the first code block doesn't work? 我对第一个代码块为何不起作用的理解是否正确? Why does wrapping it in an anonymous function make the difference? 为什么将其包装在匿名函数中会有所不同? And is there a correct (or at least better) way to chain these promises? 是否有正确(或至少更好)的方式来兑现这些承诺?

Thank you! 谢谢!

with an argument passed in, JS is executing that function call as soon as it reads it 传入参数后,JS会在读取后立即执行该函数调用

Yes. 是。 When you pass an argument, and put () behind the function, you are calling it . 当您传递参数并将()放在函数后面时, 您就在调用它 You need to pass a function instead - just like you do .then(getAdvertisers) instead of .then(getAdvertisers()) . 您需要传递一个函数-就像您执行.then(getAdvertisers)而不是.then(getAdvertisers())

Notice that it doesn't need to be a function expression, you could use .bind as well: 请注意,它不必是函数表达式,也可以使用.bind

dialogService.Confirm(null, 'Delete')
    .then(advertiserService.deleteAdvertiser.bind(advertiserService, advertiser))
    .then(getAdvertisers);

which is roughly equivalent to 大致相当于

dialogService.Confirm(null, 'Delete')
    .then(function(confirmRes) {
        return advertiserService.deleteAdvertiser(advertiser);
    })
    .then(getAdvertisers);

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

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