//
// MyPromise.swift
// WeexApp
//
// Created by zzn on 2016/9/28.
// Copyright © 2016年 zzn. All rights reserved.
//
import UIKit
import PromiseKit
class MyPromise {
init() {
setup()
}
func setup() {
login(userName: "lishi", pwd: "1234").then{self.getFriend(id: $0.id)}.then{ (list) -> Void in
print(list.enumerated())
}.catch { (err) in
print(err)
}
}
//模拟登陆
func login(userName:String,pwd:String) -> Promise<Person> {
//requestNet
let isAcc = true
let person = Person()
return Promise{ fulfile,reject in
if isAcc {
person.name = userName
person.pwd = pwd
fulfile(person)
} else {
reject(MyError.init(info: "HAHA"))
}
}
}
//模拟获取好友列表
func getFriend(id:String?) -> Promise<[Person]> {
//requestNet
let perList = [Person(),Person()]
return Promise{ fulfile,reject in
if id?.isEmpty == true {
reject(MyError.init(info: "HAHA"))
}
fulfile(perList)
}
}
}
class Person {
var name:String?
var pwd:String?
var id = "2332"
init() {
name = "zhanshan"
pwd = "11"
}
}
struct MyError:Error {
var info:String
}
网友评论