javascript - typescript 同步方法调用

标签 javascript angularjs angular typescript

你好 Nativescript 团队,

我对方法调用感到困惑。

请指导我如何在 Nativescript + Angular 中实现同步方法调用

import { Component, OnInit, AfterContentInit } from "@angular/core";

@Component({
    selector: "Home",
    moduleId: module.id,
    templateUrl: "./home.component.html",
    styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit, AfterContentInit {

constructor() {
    this.firstMethod();
    this.secondMethod();
    this.thirdMethod();
    this.fourthMethod();
}

ngOnInit(): void {
    this.firstInitMethod();
    this.secondInitMethod();
    this.thirdInitMethod();
    this.fourtInitMethod();
}

private firstInitMethod() {
    console.log("1 ::::: firstInitMethod method");
}

private secondInitMethod() {
    console.log("2 ::::: secondInitMethod method");
}

private thirdInitMethod() {
    console.log("3 ::::: thirdInitMethod method");
}

private fourtInitMethod() {
    console.log("4 ::::: fourtInithMethod method");
}


private firstMethod() {
    console.log("1 ::::: First method");
}

private secondMethod() {
    console.log("2 ::::: second method");
}

private thirdMethod() {
    console.log("3 ::::: third method");
}

private fourthMethod() {
    console.log("4 ::::: fourth method");
}


ngAfterContentInit() {
    console.log("ngaftercontnet init method called");
    this.firstAfterInitMethod();
    this.secondAfterInitMethod();
    this.thirdAfterInitMethod();
    this.fourthAfterInitMethod();
}


private firstAfterInitMethod() {
    console.log("1 ::::: firstAfterInitMethod method");
}

private secondAfterInitMethod() {
    console.log("2 ::::: secondAfterInitMethod method");
}

private thirdAfterInitMethod() {
    console.log("3 ::::: thirdAfterInitMethod method");
}

private fourthAfterInitMethod() {
    console.log("4 ::::: fourthAfterInitMethod method");
}

输出结果:

[My Phone 5508]: 1 ::::: First method
[My Phone 5508]: 2 ::::: secondInitMethod method
[My Phone 5508]: 3 ::::: thirdInitMethod method
[My Phone 5508]: 3 ::::: third method
[My Phone 5508]: 2 ::::: second method
[My Phone 5508]: 4 ::::: fourtInithMethod method
[My Phone 5508]: 4 ::::: fourth method
[My Phone 5508]: ngaftercontnet init method called
[My Phone 5508]: 1 ::::: firstAfterInitMethod method
[My Phone 5508]: 2 ::::: secondAfterInitMethod method
[My Phone 5508]: 1 ::::: firstInitMethod method
[My Phone 5508]: 3 ::::: thirdAfterInitMethod method
[My Phone 5508]: 4 ::::: fourthAfterInitMethod method

我需要输出方法同步调用:

First methods in Contructor()

        this.firstMethod();
        this.secondMethod();
        this.thirdMethod();
        this.fourthMethod();
Second methods in Init

        this.firstInitMethod();
        this.secondInitMethod();
        this.thirdInitMethod();
        this.fourtInitMethod();
Third methods in AfterInit

        this.firstAfterInitMethod();
        this.secondAfterInitMethod();
        this.thirdAfterInitMethod();
        this.fourthAfterInitMethod();

最佳答案

您必须返回一个 PromiseObservable 以等待方法完成其过程。

如:

private firstAfterInitMethod() {
  return new Promise((resolve, reject) => {
    console.log("1 ::::: firstAfterInitMethod method");
    // resolve("something"); when you want to return something.
  });
}

在您的情况下,其他方法必须返回 Promise,例如 firstAfterInitMethod()

您必须像这样调用 firstAfterInitMethod:

this.firstAfterInitMethod().then((resolve:any) => {
  // now you can call secondAfterInitMethod();
});

注意:TypeScript 和 JavaScript 是异步的。因此,当你需要做一些同步的工作时,你必须使用Promise

关于javascript - typescript 同步方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47767126/

相关文章:

javascript - 使用 Javascript 更改线性渐变不起作用

angularjs - 是否有 Yeoman 使用 e2e 测试和 $httpBackend 生成的 AngularJS 应用程序示例?

angular - 如何在 Angular 7 中从 ckeditor 5 检索数据?

javascript - 如何修改php中的json_encode函数文件?

javascript - 我需要使用 JavaScript 在所有 <td> 和 <th> 标签中插入 <span> 标签

javascript - 如何使用 Angularjs 创建自定义过滤器?

angularjs - 在angular2中显示嵌入图像

javascript - 另一个 javascript 库上的 Angular 2 templateURL 功能

http - 根据 Angular 2中先前请求的结果执行http请求

javascript - Div slider 无法随着鼠标移动而正确移动