简体   繁体   English

等到方法完成后再下一个方法?

[英]Wait until method completes before next method?

I have a class that uploads images: 我有一个上载图片的课程:

$myClass->upload();
nextMethod();

In this upload method a (AWS S3) promise is executed: 在此上传方法中,将执行(AWS S3)承诺:

$promise->then(function() { echo "Done\n"; }, function() { echo "Not Done\n"; });

How can I make it so that nextMethod is not called until the promise is completed? 我如何做到在promise完成之前不调用nextMethod?

Because the promise is asynchronous by default (ie only registering the callback to be called when the upload actually completes), the only way to do that is to execute all the logic from the promise success callback. 由于默认情况下Promise是异步的(即仅在实际完成上传时注册要调用的回调),所以唯一的方法是执行Promise成功回调中的所有逻辑。 (ie the nextMethod() ) If you require the use of variables from the parent scope, pass them to the use language construct. (即nextMethod() )如果您需要使用父作用域中的变量,请将其传递给use语言构造。

$promise->then(function() { echo "Done\n"; nextMethod(); }, function() { echo "Not Done\n"; });

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

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