将从诺言返回的值分配给全局变量 [英] Assign a value returned from a promise to a global variable

查看:71
本文介绍了将从诺言返回的值分配给全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从量角器读取浏览器的内存值,并将其存储在全局对象中.为此,我要获取window.performance.memory对象,然后解决对每个内存值进行检查的承诺.

I'm trying to read the browser memory values from Protractor and store them in a global object. To do that I'm getting the window.performance.memory object and then resolving the promise to inspect each of the memory values.

问题是我似乎无法将值分配给全局变量.我已经尝试了以下代码,但似乎效果不佳:

The problem is that I cannot seem to be able to assign the value to a global variable. I've tried the following code, which doesn't seem to work quite well:

 this.measureMemory = function () {

    var HeapSizeLimit;

    browser.driver.executeScript(function () {
        return window.performance.memory;
    }).then(function (memoryValues) {
        HeapSizeLimit = memoryValues.jsHeapSizeLimit;
        console.log('Variable within the promise: ' + HeapSizeLimit);
    });
    console.log('Variable outside the promise: ' + HeapSizeLimit);
};

这将返回:

   Variable outside the promise: undefined
   Variable within the promise: 750780416

推荐答案

因为 console.log('Variable outside the promise: ' + HeapSizeLimit);HeapSizeLimit = memoryValues.jsHeapSizeLimit;之前执行.如果在约定之后就行了,那并不意味着执行顺序是相同的.

Because console.log('Variable outside the promise: ' + HeapSizeLimit); is executed before HeapSizeLimit = memoryValues.jsHeapSizeLimit;. If it's on the line after the promise, doesn't mean the execute order is the same.

这篇关于将从诺言返回的值分配给全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆