postPersonalDetails(pdData){
let options : RequestOptionsArgs;
let token:string;
let id:string;
this.getToken().then((val) => {
token = val;
console.log("I am in token func"+token);
});
this.storage.get('userID').then((val) => {
id = val;
console.log(val);
});
console.log("I amhere"+token+id);
我在第一个consoleLog和第二个Consolelog中获取数据
由于异步性质,第三个控制台日志首先打印 我得到令牌是未定义的,用户ID是未定义的 这样做的正确方法是什么?
答案 0 :(得分:3)
角度设置附带core-js
和Promise
的polyfill。您可以将承诺与Promise.all
(see MDN with a detailed method explanation)合并,然后在两个承诺得到解决后继续。
Promise.all([
promise1, promise2, ...
])
请记住,如果其中一个合并的承诺被拒绝,则Promise.all
会拒绝。
答案 1 :(得分:0)
以下是有关PerfectPixel建议的更多细节。
class MLP(chainer.Chain):
def __init__(self, n_units, n_out):
super(MLP, self).__init__()
with self.init_scope():
# input size of each layer will be inferred when omitted
self.l1 = L.Linear(n_units) # n_in -> n_units
self.l2 = L.Linear(n_units) # n_units -> n_units
self.l3 = L.Linear(n_out) # n_units -> n_out
def __call__(self, x):
h1 = F.relu(self.l1(x))
h2 = F.relu(self.l2(h1))
return self.l3(h2)
model = MLP(args.unit, 10)
classifier_model = L.Classifier(model)
if args.gpu >= 0:
chainer.cuda.get_device_from_id(args.gpu).use() # Make a specified GPU current
classifier_model.to_gpu() # Copy the model to the GPU
# Setup an optimizer
optimizer = chainer.optimizers.AdaGrad()
optimizer.setup(classifier_model)
# --- After `optimizer.setup()`, you can modify `hyperparam` of each parameter ---
# 1. Change `update_rule` for specific parameter
# `l1` is `Linear` link, which has parameter `W` and `b`
classifier_model.predictor.l1.W.update_rule.hyperparam.lr = 0.01
# 2. Change `update_rule` for all parameters (W & b) of one link
for param in classifier_model.predictor.l2.params():
param.update_rule.hyperparam.lr = 0.01
# --- You can setup trainer module to train the model in the following...
...
这是一个更常见的工作示例。 (有关副作用的详细信息,请参阅Bergi的评论。)
const promise1 = this.getToken().then((val) => {
console.log("I am in token func"+val);
return val;
});
const promise2 = this.storage.get('userID').then((val) => {
console.log(val);
return val;
});
Promise
.all([promise1, promise2])
.then((results) => {
const [ token, id ] = results;
console.log("I am here"+token+id);
});
}