使用Observable进行AWS cognito回调 [英] Use Observable for AWS cognito callback

查看:85
本文介绍了使用Observable进行AWS cognito回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将注册回调转换为Observable,符合Angular 2约定?这就是回调结构的样子

How can I turn the signup callback into an Observable, conforming to the Angular 2 convention? This is what the callback structure is like

userPool.signUp(this.artist.email, this.artist.password, attributeList, null, function(err, result) {
  if (err) {
    alert(err);
    return;
  }
  let cognitoUser = result.user;
  console.log(JSON.stringify(result));
  console.log('user name is ' + cognitoUser.getUsername());
});


推荐答案

有一个用于此目的的RxJS静态运算符: bindNodeCallback 。它会将一个Node风格的回调API转换为一个返回Observable的函数。

There is an RxJS static operator for just that purpose: bindNodeCallback. It will convert a Node-style callback API to a function that returns an Observable.

你可以这样使用它:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/bindNodeCallback';

let signInAsObservable = Observable.bindNodeCallback(
  userPool.signUp.bind(userPool)
);
let observable = signInAsObservable(
  this.artist.email,
  this.artist.password,
  attributeList,
  null
);
observable.subscribe(
  result => {
    let cognitoUser = result.user;
    console.log(JSON.stringify(result));
    console.log('user name is ' + cognitoUser.getUsername());
  },
  error => alert(error)
);

请注意,您需要致电 bind signUp 绑定到 userPool

Note that you will need to call bind to bind signUp to userPool.

这篇关于使用Observable进行AWS cognito回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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