在使用Angular $ q进行解析调用时如何获得最后一个函数的结果?

时间:2017-10-11 18:07:31

标签: angularjs angular-promise deferred ng-dialog

我遇到的问题有点难以解释,我可能会(Angular)承诺错误,但仍然......

我正在努力处理以下情况。 一般来说,假设我希望我的Angular dialogService提供confirm方法,该方法会返回单击yes按钮时解决的承诺,这意味着确认实际成功时。但是,我希望对话框保持打开状态,直到内部async操作 - 将在yes确认时执行 - 完成。如果它成功完成,则对话框应该关闭,否则保持打开状态。

在代码中(完美地)看起来像这样:

外码:

    dialogService.confirm('title', 'message').then =>
        return myLongLastingOperationReturningPromise()

confirm方法实现如下:

    def = $q.defer()

    dialog = ngDialog.open(...)
    // closePromise or any other custom local promise
    dialog.closePromise.then =>
        // this is fake, but how can I achieve this?
        result = def.resolve('closeRequest');
        if(typeof result.then == 'function') {
            result.then =>
                // continue closing the dialog
        } else if (result === false) {
            // just do nothing
        } else {
            // closing the dialog
        }

换句话说,有没有办法在调用then之后/之后获取promises链中最后一个resolve方法的结果?

1 个答案:

答案 0 :(得分:0)

您应该在API成功返回后执行确认。

e.g。 单击“是”按钮将执行方法YesHandler:

[TestMethod]
[DataRow(10)]
[DataRow(20)]
[DataRow(30)]
public void TestMethod1(int inputValue)
{
    Assert.AreEqual(10, inputValue);
}

来电者:

import pyaudio
import json
from watson_developer_cloud import SpeechToTextV1

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 10

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)
print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

data_feed = b''.join(frames)

speech_to_text = SpeechToTextV1(
    username='secret',
    password='secret too',
    x_watson_learning_opt_out=False
)

result = speech_to_text.recognize(data_feed,
                                  content_type="audio/l16;rate=44100;channels=2",
                                  word_confidence=True,
                                  max_alternatives=4,
                                  word_alternatives_threshold=0.5,
                                  model="en-US_BroadbandModel",
                                  continuous=True)

j = json.dumps(result, indent=2)
print(j)