将变量从承诺转移到承诺

时间:2017-10-11 15:56:12

标签: javascript promise

尝试将承诺中的变量推送到另一个

function initial_view(){
    var append_data = "";

    loadViewFunctions(view_functions,append_data).then(function() {
        $("#main-content").prepend(append_data);
    });
};

function loadViewFunctions(view_functions,append_data){
    return playerHeader().then(post_app_data => {
        append_data += post_app_data;
    });
};

append_data正在返回中正确记录数据。我只是不知道如何发送它而不将其作为全局变量。

2 个答案:

答案 0 :(得分:4)

只需return then loadViewFunctions then loadViewFunction *** function initial_view(){ var append_data = ""; loadViewFunctions(view_functions,append_data).then(function(updated_data) { // *** $("#main-content").prepend(updated_data); // *** }); } function loadViewFunctions(view_functions,append_data){ return playerHeader().then(post_app_data => { return append_data + post_app_data; // *** }); } 处理程序,initial_view承诺{@ 1}}承诺接受它;请参阅function initial_view(){ var append_data = ""; loadViewFunctions(view_functions).then(function(view_data) { // *** $("#main-content").prepend(append_data + view_data); // *** }); } function loadViewFunctions(view_functions) { // *** return playerHeader(); } 注释行:

loadViewFunctions

或者在这种特殊情况下,只需在playerHeader中添加附件:

initial_view

(我假设;比问题更多;否则,只需在;中使用New-AzureADPolicy -Definition @('{"ClaimsMappingPolicy": {"Version":1,"IncludeBasicClaimSet":"true"}}') -DisplayName "AllTheClaims” -Type "ClaimsMappingPolicy" 。)

附注:功能声明(上面提到的内容)之后不需要<?php require_once __DIR__.'/vendor/autoload.php'; session_start(); require_once __DIR__ . '/assets/discord_config.php'; require_once __DIR__ . '/assets/db_config.php'; $provider = new \Discord\OAuth\Discord($discord_config); if (! isset($_GET['code'])) { header('Location: '. $provider->getAuthorizationUrl()); } else { $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'], ]); // Get the user object. $user = $provider->getResourceOwner($token); // Get the guilds and connections. $guilds = $user->guilds; foreach ($guilds as $server) { if($server->owner) { $_SESSION['discord']['servers'][$server->id] = array( 'id' => $server->id, 'name' => $server->name, 'icon' => $server->icon ); } } //Add user to DB or get user ID from db $mysql = new mysqlHelper($conn); $mysql->add_user($user->username, $user->email, $user->discriminator); $_SESSION['discord']['user']['id'] = $mysql->get_user_id($user->email); $_SESSION['discord']['user']['name'] = $user->username; $_SESSION['discord']['user']['email'] = $user->email; $_SESSION['discord']['user']['discriminator'] = $user->discriminator; //Redirect to main page header('Location: index.php'); exit(); } WrapMode语句 -terminator,但声明不是语句。

答案 1 :(得分:1)

您的问题是,您未从playerHeader().then()回调中返回值。

function initial_view(){
    var append_data = "";

    loadViewFunctions(view_functions,append_data).then(function(modifiedAppendData) {
        $("#main-content").prepend(modifiedAppendData);
    });
};

function loadViewFunctions(view_functions,append_data){
    return playerHeader().then(post_app_data => {
        append_data += post_app_data;
        return append_data;
    });
};

Promise允许您链接then / catch个电话,但它们也允许您在每个电话之间修改数据。您原始示例中发生的事情是,您的按时间顺序排列的第一个then调用返回未定义,因此这是第二个then作为参数接收的内容。