首先执行Javascript,promise.then()函数

时间:2018-04-08 07:36:56

标签: javascript promise

我提前道歉,因为忽略了一些根本性的事情,但我无法理解以下问题。

我想在服务器上创建一个用户,随后显示所有用户(包括新用户)。为了简化事情,我在"然后"条款。问题是, 警报出现在" CreateUser"在服务器上已经完成。根据文件,"然后"在承诺完成时执行。 为什么它在我的电脑上反过来?

function createUser(newUser) {   
    userService.create(newUser) // Post creating new user
        .then(      
        alert('Why do I come up before completion of the  userService.create() function  ?')
        );  
}

这是userService.create()函数:

function create(user) {
//    alert(user);
    const requestOptions = {
        method: 'POST',
        headers: { ...authHeader(), 'Content-Type': 'application/json' },
        body: user
    };

    return fetch('/Client/Create', requestOptions).then(handleResponse, handleError);
}

用handleResponse:

function handleResponse(response) {
    return new Promise((resolve, reject) => {

....

的HandleError:

function handleError(error) {
    return Promise.reject(error && error.message);
}

万分感谢。

2 个答案:

答案 0 :(得分:2)

变化

Public Class UTextBoxWatermark
    Inherits TextBox

    '============================================================================
    'CONSTANTS.
    '============================================================================
    Const WM_PAINT As Integer = &HF

    '============================================================================
    'VARIABLES.
    '============================================================================
    Private gsWatermarkText As String = "Watermark"
    Private glWatermarkColor As Color = Color.Gray

    '============================================================================
    'PROPERTIES.
    '============================================================================
    Public Property WatermarkText As String
        Get
            Return gsWatermarkText
        End Get
        Set(sValue As String)
            gsWatermarkText = sValue
            Me.Invalidate()
        End Set
    End Property

    Public Property WatermarkColor As Color
        Get
            Return glWatermarkColor
        End Get
        Set(lValue As Color)
            glWatermarkColor = lValue
            Me.Invalidate()
        End Set
    End Property

    '============================================================================
    'CONSTRUCTORS AND DESTRUCTORS.
    '============================================================================
    Public Sub New()
        'This call is required by the designer.
        InitializeComponent()
    End Sub

    '============================================================================
    'EVENT HANDLERS.
    '============================================================================
    Protected Overrides Sub WndProc(ByRef m As Message)
        MyBase.WndProc(m)

        Dim oBrush As SolidBrush

        If m.Msg = WM_PAINT Then
            'If the text is empty now, the watermark text should be written 
            'instead.
            If Me.Text.Length = 0 Then
                oBrush = New SolidBrush(glWatermarkColor)
                Using oGraphics As Graphics = Me.CreateGraphics
                    oGraphics.DrawString(gsWatermarkText, Me.Font, oBrush, 0, 0)
                End Using
            End If
        End If
    End Sub

    Private Sub UTextBoxWatermark_TextChanged(sender As Object, e As EventArgs) _
        Handles Me.TextChanged

        If Me.Text.Length = 0 Then
            Me.Invalidate()
        End If
    End Sub
End Class

为:

function createUser(newUser) {   
    userService.create(newUser) // Post creating new user
        .then(      
        alert('Why do I come up before completion of the  userService.create() function  ?')
        );  
}

then

答案 1 :(得分:1)

then需要传递一个函数,所以这样做:

userService.create(newUser).then(() => {
    alert('Why do I come up before completion of the  userService.create() function  ?')
});