Skip to content

Commit

Permalink
chore(taro-rn): 修改storage api中Promise的调用方式
Browse files Browse the repository at this point in the history
  • Loading branch information
Bless-L committed Nov 21, 2018
1 parent 2c92757 commit 4f0a38c
Showing 1 changed file with 64 additions and 56 deletions.
120 changes: 64 additions & 56 deletions packages/taro-rn/src/api/storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,80 +5,88 @@ export function setStorage (opts = {}) {
const { key, data, success, fail, complete } = opts
const res = { errMsg: 'setStorage:ok' }

return AsyncStorage.setItem(key, JSON.stringify(data))
.then((e) => {
success && success(res)
complete && complete(res)

return Promise.resolve(res)
}).catch((err) => {
res.errMsg = err.message
fail && fail(res)
complete && complete(res)

return Promise.reject(err)
})
return new Promise((resolve, reject) => {
AsyncStorage.setItem(key, JSON.stringify(data))
.then((e) => {
success && success(res)
complete && complete(res)

resolve(res)
}).catch((err) => {
res.errMsg = err.message
fail && fail(res)
complete && complete(res)

reject(err)
})
})
}

export function getStorage (opts = {}) {
const { key, success, fail, complete } = opts
const res = { errMsg: 'getStorage:ok' }

return AsyncStorage.getItem(key)
.then((data) => {
res.data = JSON.parse(data)
success && success(res)
complete && complete(res)

return Promise.resolve(res)
}).catch((err) => {
res.errMsg = err.message
fail && fail(res)
complete && complete(res)

return Promise.reject(err)
})
return new Promise((resolve, reject) => {
AsyncStorage.getItem(key)
.then((data) => {
res.data = JSON.parse(data)
success && success(res)
complete && complete(res)

resolve(res)
}).catch((err) => {
res.errMsg = err.message
fail && fail(res)
complete && complete(res)

reject(err)
})
})
}

export function getStorageInfo (opts = {}) {
const { success, fail, complete } = opts
const res = { errMsg: 'getStorageInfo:ok' }

return AsyncStorage.getAllKeys()
.then((data) => {
res.keys = data
res.currentSize = null
res.limitSize = null
success && success(res)
complete && complete(res)

return Promise.resolve(res)
}).catch((err) => {
res.errMsg = err.message
fail && fail(res)
complete && complete(res)

return Promise.reject(err)
})
return new Promise((resolve, reject) => {
AsyncStorage.getAllKeys()
.then((data) => {
res.keys = data
res.currentSize = null
res.limitSize = null
success && success(res)
complete && complete(res)

resolve(res)
}).catch((err) => {
res.errMsg = err.message
fail && fail(res)
complete && complete(res)

reject(err)
})
})
}

export function removeStorage (opts = {}) {
const { key, success, fail, complete } = opts
const res = { errMsg: 'removeStorage:ok' }

return AsyncStorage.removeItem(key)
.then(() => {
success && success(res)
complete && complete(res)

return Promise.resolve(res)
}).catch((err) => {
res.errMsg = err.message
fail && fail(res)
complete && complete(res)

return Promise.reject(err)
})
return new Promise((resolve, reject) => {
AsyncStorage.removeItem(key)
.then(() => {
success && success(res)
complete && complete(res)

resolve(res)
}).catch((err) => {
res.errMsg = err.message
fail && fail(res)
complete && complete(res)

reject(err)
})
})
}

export async function clearStorage () {
Expand Down

0 comments on commit 4f0a38c

Please sign in to comment.