Created
September 22, 2018 14:11
-
-
Save synga/a08353f53d51fdb12d2c99f3e6bca4a1 to your computer and use it in GitHub Desktop.
Transformando um metodo para retornar promise.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//função de cadastro de cliente | |
adicionarCliente() { | |
// NÃO FAÇO A MINIMA IDEIA DO QUE FAZ ESSE METODO ABAIXO, MAS SE ALGO APÓS ELE RETORNA UM VALOR NECESSÁRIO PARA CUMPRIR... | |
// ...ESSE METODO DE ADICIONAR CLIENTE, O CERTO SERIA TRANSFORMAR EM PROMISE TAMBÉM. | |
this.cliente.clienteFinalizado = true; | |
this.clienteProvider.adicionarCliente(this.cliente); | |
//faz o upload da foto | |
this.uploadPhoto().then(response => { | |
// após upload concluído, a variável foto recebe a URL da foto no storage | |
// PEGA A URL DA FOTO QUE RETORNOU NA PROMESA E ATRIBUI A FOTO DO CLIENTE | |
this.cliente.foto = response; | |
this.toastCadastro(); | |
}).catch(err => { | |
// tratar seu erro aqui | |
console.log(err); | |
}); | |
} | |
//função do upload de fotos | |
// ESSE METODO DEVE SER DO TIPO Promise<any> POIS VOCÊ RETORNARÁ OU A STRING DA FOTO OU UM OBJETO DE ERRO | |
private uploadPhoto = (): Promise<any> => { | |
// RETORNA UMA NOVA PROMESA QUE, SE DER resolve() CAI EM SUCESSO, SE DER reject() CAI NO CATCH DE ERRO | |
return new Promise<any>((resolve, reject) => { | |
this.myPhotosRef.child(this.generateUUID()).child('myPhoto.png') | |
.putString(this.Picture, 'base64', { contentType: 'image/png' }) | |
.then((savedPicture) => { | |
// aqui a myPhotoURL recebe a URL da foto | |
// !!!NÃO SEI SE USA myPhotoURL PARA ALGUMA OUTRA COISA ENTÃO MANTIVE AQUI | |
this.myPhotoURL = savedPicture.downloadURL; | |
resolve(savedPicture.downloadURL) | |
}, err => reject(err)); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment