Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在返回值之前等待其他事件完成?_Javascript_Angular_Typescript_Angular2 Services - Fatal编程技术网

Javascript 如何在返回值之前等待其他事件完成?

Javascript 如何在返回值之前等待其他事件完成?,javascript,angular,typescript,angular2-services,Javascript,Angular,Typescript,Angular2 Services,我有一个HttpClient服务,它负责所有rest调用。 现在,在进行rest调用之前,我想加载一个iframe,然后继续相同的函数调用 public get(url, searchParams: URLSearchParams = new URLSearchParams(), headers: Headers = new Headers() ) { if (some condition) { let iframe = this.createIframe(); retur

我有一个HttpClient服务,它负责所有rest调用。 现在,在进行rest调用之前,我想加载一个iframe,然后继续相同的函数调用

public get(url, searchParams: URLSearchParams =  new URLSearchParams(), headers: Headers = new Headers() ) {
  if (some condition) {
    let iframe = this.createIframe();
    return new Promise((resolve) =>
      setTimeout(() => {
        iframe.parentNode.removeChild(iframe);
        resolve(this.doGET(url, this.getOptions(searchParams, headers))); 
    }, this.time_delay));
  } else {
    return this.doGET(url, this.getOptions(searchParams, headers));
  }
}
下面是createIframe方法:

private createIframe() {
    let iframe = document.createElement('iframe');
    iframe.style.display = 'none';
    this.createSomeUrl('').then(function (url) {
         iframe.src = url;
    });
    document.body.appendChild(iframe);
    return iframe;
 }
在这里,我使用
setTimeout
使
doGet()
调用等待一段时间,但它不能保证
iframe
完成其工作


有一个变量在
iframe
完成其工作时得到更新,我可以通过检查该变量知道
iframe
已完成其工作,但此时如何停止get方法等待返回承诺?

编辑

private checkSomeConditions(): Promise {
    return new Promise( resolve => {
        if (some condition) {
            let iframe = this.createIframe();

            // Assuming the iframe is closing after the job is done
            iframe.onbeforeunload( _ => {
               console.log('iframe is closing');
               resolve();
            });
        } else resolve();
    });
}

public get(url, searchParams: URLSearchParams =  new URLSearchParams(), headers: Headers = new Headers() ) {
    return this.checkSomeConditions()
    .then( _ -> {
        return this.doGET(url, this.getOptions(searchParams, headers);
    })
    [...]
}

如果您使用的是可观察对象,则可以使用
flatMap()
轻松完成此操作:


现在,当您订阅
.get()
方法时,如果
someCondition
为true,它将延迟,否则返回。

使用$timeout函数回答问题时,您应该解释您正在做什么,而不是仅仅发布简单的代码。这更有可能帮助提问者了解未来。Alexis感谢你的帮助,但我在这里试图避免使用超时,我想确保iframe在进行doGet call.Wooops之前已经完成了工作。完全误解了你的问题,很抱歉。您可以根据需要(例如:iframe.onload=function(){//Loaded};)侦听iframe上发生的事件,而不是使用timeout。假设iframe在作业完成后自动关闭,您也可以侦听“onbeforeunload”。编辑我的回答谢谢你的帮助,但我在这里试图避免使用时间延迟,我想确保iframe在进行doGet调用之前已经完成了它的工作。
public get(url, searchParams: URLSearchParams = new URLSearchParams(), headers: Headers = new Headers()) {
    if (somecondition) {
        let iframe = this.createIframe();

        return Observable
            .delay(this.time_delay)
            .flatMap(() => {
                iframe.parentNode.removeChild(iframe);
                return this.doGET(url, this.getOptions(searchParams, headers));
            })
    }
    else {
        return this.doGET(url, this.getOptions(searchParams, headers));
    }
}